start node server 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: how to run a js file in node
Save your .js file in the same folder as node.js,
For windows , Usually location is- "C:\Program Files\nodejs"
Open 'Command prompt and locate node.js folder' OR directly open 'Node.js command prompt' and type
>> node (file_name).js