showing results for - "nodejs server code"
Lyndon
03 Apr 2016
1/* ====== create node.js server with core 'http' module ====== */
2// dependencies
3const http = require("http");
4
5// PORT
6const PORT = 3000;
7
8// server create
9const server = http.createServer((req, res) => {
10   if (req.url === "/") {
11      res.write("This is home page.");
12      res.end();
13   } else if (req.url === "/about" && req.method === "GET") {
14      res.write("This is about page.");
15      res.end();
16   } else {
17      res.write("Not Found!");
18      res.end();
19   }
20});
21
22// server listen port
23server.listen(PORT);
24
25console.log(`Server is running on PORT: ${PORT}`);
26
27/* ========== *** ========== */
28
29/* ====== create node.js server with express.js framework ====== */
30// dependencies
31const express = require("express");
32
33const app = express();
34
35app.get("/", (req, res) => {
36   res.send("This is home page.");
37});
38
39app.post("/", (req, res) => {
40   res.send("This is home page with post request.");
41});
42
43// PORT
44const PORT = 3000;
45
46app.listen(PORT, () => {
47   console.log(`Server is running on PORT: ${PORT}`);
48});
49
50
51// ======== Instructions ========
52// save this as index.js
53// you have to download and install node.js on your machine
54// open terminal or command prompt
55// type node index.js
56// find your server at http://localhost:3000
Valentín
22 Oct 2017
1const http = require('http');
2
3const hostname = '127.0.0.1';
4const port = 3000;
5
6const server = http.createServer((req, res) => {
7  res.statusCode = 200;
8  res.setHeader('Content-Type', 'text/plain');
9  res.end('Hello World');
10});
11
12server.listen(port, hostname, () => {
13  console.log(`Server running at http://${hostname}:${port}/`);
14});
queries leading to this page
open port for node jsnodejs servernode js change portnode js listen to portbasic node js servernodejs web serverhow to create server in node jsnode serversimple nodejs servercreate a server in jscreate node servernode set portnode js set portnode js server creationmake an js serverhow to start my node servernodejs serverstart node js servernod js hello world programnodejs serverhello world nodejsnode js create serverhow to start my server on nodecreate server nodejsstarting node serverserver js nodenode create serverbasic node serverbasic nodejs serverbuilding a web server with nodejshow to creat server node jshttp nodejs serverhello world node jsnode web servernodejs server codecreate a simple node servernodejs basic http serverhttp server node jshow to run node js hello worldnodejs hello world examplenodejs hello worldnode hello worldnode js serverprogram node to create a servernode js start servernode js server creationhow to start a server in node jshow to start a node js servernodejs basic servercreating a server in nodehow to create server using nodejssimple node server codenode js client portnod js create serverhow to create a web server node jscreate server in node jssimple node serverhttp node js serverhow to create web server in node jsnodejs example serversimple nodejs server setuphow to start node serverhttp server node js nodejs servercreate a server with node jsnodejs create serverhow to start a node servernodejs servernodejs example server codehow to create a server in node jscreate node js serverwhat does create server do in nodenode js app set portcreate nodejs serverhello world node apphow to make a interactive web server in jsserver port node jsnodejs servernode js serversetting node servercreate server with node jsmake a node js server tutorialhello world program in node jsnode js not listening porthow to make a nodejs servernodejs server examplenode js hello world programsimple program of node creating a serverhow to print hello world in node jscreate http server nodejshow to create node servernode js hello worldsetting up a node serverhow to get port of node appnodejs serverhow to get port on which node is runninghow to set up nodejs serverhello world in node jssimplest node js servernodejs servercreate server node jsset up node servernode listen portnode js set port manuallynode js server startrun node js servernode js server setupbasic node 3bjs servercreate a node js project with hello worldhow to set up a basic node js servercreating server in node jsnodejs servernode js server createnode js web servernodejs serverwhich port node app listen on linux servernode hello world examplenodejs server creationnodejs servernode js hello world examplehow to start the node serverport nodejs appnodejs servercreate a server in node jsnodejs create a servernode js server scriptcreate node js servernodejs server setuphow to create node js serverjs web server exampleserver nodehow to set up node serverhow to setup node js serverbasic node js servernode server examplenode js create a servernode server jshttp node js serverport listen node jsnode app set porthow to create a server with node jsstart node servernodejs serverhow to create node js servernodejs server with custom portnode server codemake node serverhow to create a web server in node jshow to start node js serverhow to create a server in nodejavascript node create servernodejs server code