-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (51 loc) · 1.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const http = require("http");
const fs = require("fs");
const url = require("url");
const express = require("express");
//---Working with express---
const app = express();
//Create handler in express
app.get("/", (req, res) => {
res.end("Hello on the Home page");
});
app.get("/about", (req, res) => {
res.end("Hello! I am a developer.");
});
//Create http server using express
app.listen(8005, () => "Server started!");
//---Working natively in Node.js---
//Http request handler
function httpRequestHandler(req, res) {
//Not log this request
if (req.url === "/favicon.ico") return res.end();
//Log incoming request
const log = `${Date.now()}: ${req.method}: ${
req.url
}: New request received\n`;
//Parse url
const parseURL = url.parse(req.url);
console.log("parsed URL components: ", parseURL);
fs.appendFile("log.txt", log, (err, data) => {
switch (parseURL.path) {
case "/":
res.end("Home page");
break;
case "/about":
res.end("About page: I am developer");
break;
case "/signup":
if (req.method === "Get") {
res.end("This is a signup form.");
} else if (req.method === "Post") {
//Db query
res.end("successfully");
}
default:
res.end("404 Not Found");
break;
}
});
}
//Create http server nativley
const nativeServer = http.createServer(httpRequestHandler);
nativeServer.listen(8004, () => "Server started!");