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
1
2//HTTP MODULE NODE.JS
3var http = require('http');
4var server = http.createServer(function(req, res){
5 //write code here
6});
7server.listen(5000);
1var http = require('http');
2
3//create a server object:
4http.createServer(function (req, res) {
5 res.write('Hello World!'); //write a response to the client
6 res.end(); //end the response
7}).listen(8080); //the server object listens on port 8080