forked from burhanuddinbaig/vanilla-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
47 lines (40 loc) · 1.43 KB
/
app.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
const express = require("express");
const body_parser = require("body-parser");
const app = express(); //to call function reference express()
const PORT = 3000 || 4000; //Port may be 3000 or 4000
app.use(body_parser.urlencoded({extended:false}))
const userController = require("./controller/user"); //to import User Controller
const userRouter = require("./routes/users") //to import User Router
// User Routes
app.use("/api/user", userRouter.router);
// Home Route
app.use("/home", (req,res,next)=>{
let Arr = userController.Arr
let arrLen = Arr.length
if(arrLen > 0){
res.setHeader("Content-Type", "text/html");
res.write("<h1>Home Page</h1>")
for (let i = 0; i < arrLen; i++){
res.write(`
<h4>
- ${Arr[i]}
<form action='/api/user/Delete/${i}' method='Get'>
<button type='submit'>DELETE</button>
</form>
</h4>
`)
}
res.write("<a href='/api/user/addUser'>Add New</a>")
}
else{ // to redirect to add user if no user found
res.redirect("/api/user/addUser")
}
res.end();
});
// Not found route
app.all("*", function(req,res){
res.write("Not Found");
res.end();
});
// To log the server running status
app.listen(PORT, console.log(`Server is running at localhost:${PORT}`));