Skip to content

Instantly share code, notes, and snippets.

@jayesh96
Last active September 4, 2021 09:49
Show Gist options
  • Save jayesh96/3f39cc098255f2f288a5da6ecbcf1ca9 to your computer and use it in GitHub Desktop.
Save jayesh96/3f39cc098255f2f288a5da6ecbcf1ca9 to your computer and use it in GitHub Desktop.
Running a server on port 3000
// Step1: create a package
npm init
// Step2: create index.js file in same folder as package.json and copy this code
const http = require('http');
// Create an instance of the http server to handle HTTP requests
let app = http.createServer((req, res) => {
// Set a response type of plain text for the response
res.writeHead(200, {'Content-Type': 'text/plain'});
// Send back a response and end the connection
res.end('Hello World!\n');
});
// Start the server on port 3000
app.listen(3000, '127.0.0.1');
console.log('Node server running on port 3000');
// Step3: Add the start scripts inside package.json
{
"name": "desktop",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start":"node index.js", // This script
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
// Step4: Execute npm start from terminal
npm start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment