首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSON服务器路由器

JSON服务器路由器
EN

Stack Overflow用户
提问于 2016-06-06 07:23:59
回答 1查看 3.3K关注 0票数 1

我正在尝试让聚合网页登录工作,但似乎我做不到,因为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文件

代码语言:javascript
复制
    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代码项目文件夹结构的图片

EN

回答 1

Stack Overflow用户

发布于 2016-08-20 03:29:57

您可能需要在代码中添加中间件,如下所示:

代码语言:javascript
复制
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中间件中注入中间件,如下所示:

代码语言:javascript
复制
server.use(middlewares); //logger, static and cors middleware
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37651683

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档