1#1. server.js
2var express = require('express');
3var app = express();
4
5app.get('/', function (req, res) {
6 res.status(200).send('Hello World!');
7});
8
9var port = process.env.PORT || 3000;
10
11var server = app.listen(port, function() {
12 console.log('Express server listening on port ' + port);
13});
14
15open cmd run server.js
16"node server.js"
17log:Express server listening on port 3000
18
19& then
20
21open link "http://localhost:3000/" in your browser and show result.
1//Load HTTP module
2const http = require("http");
3const hostname = '127.0.0.1';
4const port = 3000;
5
6//Create HTTP server and listen on port 3000 for requests
7const server = http.createServer((req, res) => {
8
9 //Set the response HTTP header with HTTP status and Content type
10 res.statusCode = 200;
11 res.setHeader('Content-Type', 'text/plain');
12 res.end('Hello World\n');
13});
14
15//listen for request on port 3000, and as a callback function have the port listened on logged
16server.listen(port, hostname, () => {
17 console.log(`Server running at http://${hostname}:${port}/`);
18});
19