how to start a node server code example
Example 1: SERVER PORT NODE.JS
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Example 2: how to create node js server
//HTTP MODULE NODE.JS
var http = require('http');
var server = http.createServer(function(req, res){
//write code here
});
server.listen(5000);
Example 3: create node js server
import express from 'express';
const server = express();
const port = 8080;
server.get('/', (req, res) => {
return res.send('Hello, Express.js!');
})
server.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Example 4: 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 5: node command
> node
Welcome to Node.js <version>.
>'type in javascript'
Example 6: javascript running at node
const inNode = new Function('try{return this===global;}catch(err){return false;}')();