-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyApp.js
66 lines (54 loc) · 1.45 KB
/
myApp.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
62
63
64
65
66
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
/*app.use(function middleware(req,res,next){
console.log(req.method + " " + req.path + " - " + req.ip);
next();
});*/
// express hello world
/*app.get("/",function(req,res)
{
res.send("Hello Express");
});*/
// attaching the assets files i.e. css,js and bootstrap files
app.use("/public",express.static(__dirname + "/public"));
app.get("/",function(req,res)
{
absolutePath = __dirname + "/views/index.html";
res.sendFile(absolutePath);
});
// return json object
/*app.get("/json",function(req,res)
{
if(process.env.MESSAGE_STYLE == "uppercase")
{
res.json({"message": "HELLO JSON"});
}
else
{
res.json({"message": "Hello json"});
}
});*/
// applying middleware function with chain
/*app.get("/now",function(req,res,next){
req.time = new Date().toString();
next();
},function(req,res){
res.json({"time":req.time});
});*/
//URLs parameters
/*app.get("/:word/echo",function(req,res){
res.json({"echo":req.params.word});
});*/
//query string using route, get and post method
/*app.get("/name",function(req,res){
req.firstName = req.query.first;
req.lastName = req.query.last;
res.json({name : `${req.firstName} ${req.lastName}`});
});*/
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
app.post("/name",function(req,res){
res.json({"name": req.body.first+" "+req.body.last});
});
module.exports = app;