locally host a node .js server express code example
Example 1: Create a server which serves at port 3000 and with node.js command prompt
const http = require('http');
let app = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!\n');
});
app.listen(3000, '127.0.0.1');
console.log('Node server running on port 3000');
$ node app.js
Example 2: create express server local
'use strict';
const express = require('express');
const app = express();
app.use(express.static('public'));
app.use(express.json());
app.get('/', function(req,res){
res.send('This is the Homepage');
});
app.listen(3000);