我正在尝试让聚合网页登录工作,但似乎我做不到,因为app.js无法读取文件本身定义的JSON数据库。我已经上传了一个关于我的文件夹和文件是如何在Visual代码中分层的屏幕截图。我使用Windows 10 NT操作系统和Git Bash来运行我的命令。
这是GIT BASH错误
/c/users/rhino/documents/work/personal/polymer-project @桌面-NB42TJJ MINGW64 MINGW64节点演示- Server /app.jsJSONServer正在运行TypeError:无法读取未定义的属性“用户”在下一个(C:\users\rhino\documents\work\personal\polymer-project\node_modules\express\lib的Layer.handle 请求上(C:\users\rhino\documents\work\personal\polymer-project\node_modules\express\lib\router\route.js:112:3) at Layer.handle 请求 at C:\users\rhino\documents\work\personal\polymer-project\node_modules\express\lib\router\index.js:277:22 at Function.process_params (C:\users\rhino\documents )\work\personal\polymer-project\node_modules\express\lib\router\index.js:330:12) at next (C:\users\rhino\documents\work\personal\polymer-project\node_modules\express\lib\router\index.js:271:10) at C:\users\rhino\documents\work\personal\polymer-project\demo-server\app.js:29:9 at Layer.handle 请求
这是我的app.js文件
var express = require("../node_modules/express");
var app = express();
var path = require("path");
var jsonServer = require("../node_modules/json-server");
var server = jsonServer.create();
var router = jsonServer.router('db.json');
//Authentication Libraries - Start
var cookieParser = require('../node_modules/cookie-parser');
var session = require('../node_modules/express-session');
//Authentication Libraries - End
server.use(cookieParser("security", {"path": "/"}));
app.use(cookieParser("security", {"path": "/"}));
server.use(function(req, res, next) {
res.setHeader("Access
-Control-Allow-Origin", "http://localhost:8080");
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS");
res.setHeader("Access-Control-Expose-Headers","Access-Control-Allow-Origin");
res.setHeader("Access-Control-Allow-Headers",
"X-Custom-Header,X-Requested-With,X-Prototype-Version,Content-Type,Cache- Control,Pragma,Origin,content-type");
if (!req.signedCookies.usersession && req._parsedUrl.pathname != "/auth/login" && req.method != "OPTIONS") {
res.redirect('http://localhost:8080/app/pages/auth/auth.html');
}else{
next();
}
});
server.post('/auth/login', function(req, res){
var users = router.db.object.users;
var username = req.query.username;
var password = req.query.password;
for(var i=0;i<=users.length -1;i++){
if(users[i].username == username && users[i].password == password) {
res.cookie('usersession', users[i].id, {maxAge: 9000000, httpOnly: false, signed: true});
res.send(JSON.stringify({success: true}));
return;
}
}
res.send(JSON.stringify({ success: false, error: 'Wrong username or password' }));
});
app.get('/', function(req, res){
if (!req.signedCookies.usersession) {
res.redirect('app/pages/auth/auth.html');
}else{
res.sendFile(path.join(__dirname+'/../app/index.html'));
}
});
app.get('/auth/logout', function(req, res){
res.clearCookie('usersession');
res.redirect('/app/pages/auth/auth.html');
});
/*app.get('/', function(req, res){
res.sendFile(path.join(__dirname+'/../app/index.html'));
});
*/
app.use(express.static(path.join(__dirname, '../')));
var http = require('http').Server(app);
http.listen(8080);
server.use(jsonServer.defaults); //logger, static and cors middlewares
server.use(router); //Mount router on '/'
server.listen(5000, function () {
console.log('JSON Server is runnning')
});Visual代码项目文件夹结构的图片
发布于 2016-08-20 03:29:57
您可能需要在代码中添加中间件,如下所示:
var jsonServer = require('../node_modules/json-server');
var server = jsonServer.create();
var router = jsonServer.router('db.json');
var middlewares = jsonServer.defaults(); //<--- new line然后在server.use(jsonServer.defaults);//logger中,静态和cors中间件中注入中间件,如下所示:
server.use(middlewares); //logger, static and cors middlewarehttps://stackoverflow.com/questions/37651683
复制相似问题