how to create node js code example

Example 1: create node in javascript

<div id="div1">
  <p id="p1">This is a paragraph.</p>
  <p id="p2">This is another paragraph.</p>
</div>

<script>
  // Create <p> element
  var para = document.createElement("p");
  // Create a text node
  var node = document.createTextNode("This is new.");
  // Append text node to <p> element
  para.appendChild(node);

  // Append <p> to div element
  var element = document.getElementById("div1");
  element.appendChild(para);
</script>

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: node app create

using command line >>> 

npm init

open text editor create a js file. for example like index.js and paste following code.

>>> 
  
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}/`);
});

----

run using 
node app.js

Example 4: javascript running at node

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