node server start command 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: srart server js
require('dotenv').config();
const express = require('express');
const server = express();
const cors = require('cors');
const ejs = require('ejs');
const methodOverride = require('method-override');
const pg = require('pg');
const agent = require('superagent');
const client = new pg.Client(process.env.DATABASE_URL);
server.use(methodOverride('_method'));
server.use(express.static('./public'));
server.use(cors());
server.use(express.urlencoded({extended :true}));
server.set('view engine','ejs');
Example 3: node command
> node
Welcome to Node.js <version>.
>'type in javascript'