-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
66 lines (52 loc) · 1.61 KB
/
Copy pathserver.js
File metadata and controls
66 lines (52 loc) · 1.61 KB
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 app = express();
var port = process.env.PORT || 8080;
var bodyParser = require('body-parser');
var router = express.Router();
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
var path = require('path');
app.use(express.static(path.join(__dirname, 'public')));
// route middleware that will happen on every request
router.param('name',function(req,res,next,name){
if(name.length>5){
console.log("Valid Name");
}
else{
console.log("invalid name");
}
req.name = name;
next();
})
router.use(function(req, res, next) {
// log each request to the console
console.log(req.method, req.url);
// continue doing what we were doing and go to the route
next();
});
router.get('/hello/:name',function(req,res){
res.send('Hello'+req.params.name);
});
router.get('/', function(req, res) {
res.send('im the home page!');
});
router.get('/about', function(req, res) {
res.send('im the about page!');
});
app.route('/login')
.get(function(req, res) {
res.sendFile(__dirname + '/public/views/login.html');
})
.post(function(req, res) {
console.log('processing');
res.send('processing the login form!');
});
app.use('/', router);
// app.post('/api/users', function(req, res) {
// var user_id = req.body.id;
// var token = req.body.token;
// var geo = req.body.geo;
// res.send(user_id + ' ' + token + ' ' + geo);
// });
app.listen(port);
console.log('Server started! At http://localhost:' + port);