how to run a node app code example

Example 1: node js server

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))

Example 2: create node project

#1. server.js
var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.status(200).send('Hello World!');
});

var port = process.env.PORT || 3000;

var server = app.listen(port, function() {
  console.log('Express server listening on port ' + port);
});

open cmd run server.js
"node server.js"
log:Express server listening on port 3000

& then

open link "http://localhost:3000/" in your browser and show result.

Example 3: 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 4: javascript running at node

const inNode = new Function('try{return this===global;}catch(err){return false;}')();

Tags:

Misc Example