-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
75 lines (63 loc) · 2.08 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//demo.js 文件
var express = require('express'),
hbs = require('hbs'),
cookieParser = require('cookie-parser'),
session = require('express-session'),
fs = require('fs'),
http = require('http'),
app = express();
app.use(cookieParser());
app.use(session({
secret: '123456',
cookie: {maxAge: 60 * 1000 * 30},
resave: false,
saveUninitialized: true
}));
app.use(express.static(__dirname + '/src/static'));
app.get('/hello', function (req, res) {
res.send('HELLO');
});
var user = require(__dirname + '/src/server/user.js');
app.get('/login', function (req, res) {
if (req.session.sign) {//检查用户是否登录
res.send('welecome <strong>' + req.session.name + '</strong>, 欢迎你再次登录');
} else {
user.login('王冲', '111qqq',function(userInfo){
req.session.sign = true;
req.session.name = userInfo.userName;
res.send('欢迎登录,' + req.session.name);
});
}
});
//app.get('/index', function (req, res) {
// console.log(__dirname);
// res.sendFile(__dirname + '/src/views/' + 'index.html');
//});
app.get('/process_get', function (req, res) {
var response = {
firstName: req.query.firstName,
lastName: req.query.lastName
}
console.log(response);
res.end(JSON.stringify(response));
});
hbs.registerPartials(__dirname + '/src/views/partials');
app.set('view engine', 'hbs');
app.set('views', __dirname + '/src/views/');
app.get('/news', function (req, res) {
var userInfo = {
userName: req.session.name
}
res.render('index', {title: 'hbs news demo', author: 'qujh',userInfo:userInfo});
});
app.get('/about/ygt', function (req, res) {
res.render('about/test', {title: 'hbs ygt demo', author: 'qujh'});
});
app.get('/about/about', function (req, res) {
res.render('about/about', {title: 'hbs about demos', author: 'qujh'});
});
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("应用实例,访问地址为 http://%s:%s", host, port);
});