【问题标题】:JSON Server routerJSON 服务器路由器
【发布时间】:2016-08-20 11:29:57
【问题描述】:

我正在尝试让聚合物网页登录工作,但似乎我做不到,因为 app.js 无法读取文件本身中定义的 JSON 数据库。我已经上传了我的文件夹和文件如何在 Visual Studio Code 中分层的屏幕截图。我正在使用 Windows 10 NT 操作系统和 Git Bash 来运行我的命令。

这是 GIT BASH 错误

Rhino@DESKTOP-NB42TJJ MINGW64 /c/users/rhino/documents/work/personal/polymer-project $节点 demo-server/app.js JSON 服务器正在运行 TypeError:无法读取 未定义的属性“用户” 在 C:\users\rhino\documents\work\personal\polymer-project\demo-server\app.js:34:33 在 Layer.handle [as handle_request] (C:\users\rhino\documents\work\personal\polymer-project\node_modules\express\lib\router\layer.js:95:5) 在下一个(C:\users\rhino\documents\work\personal\polymer-project\node_modules\express\lib\router\route.js:131:13) 在 Route.dispatch (C:\users\rhino\documents\work\personal\polymer-project\node_modules\express\lib\router\route.js:112:3) 在 Layer.handle [as handle_request] (C:\users\rhino\documents\work\personal\polymer-project\node_modules\express\lib\router\layer.js:95:5) 在 C:\users\rhino\documents\work\personal\polymer-project\node_modules\express\lib\router\index.js:277:22 在 Function.process_params (C:\users\rhino\documents\work\personal\polymer-project\node_modules\express\lib\router\index.js:330:12) 在下一个(C:\users\rhino\documents\work\personal\polymer-project\node_modules\express\lib\router\index.js:271:10) 在 C:\users\rhino\documents\work\personal\polymer-project\demo-server\app.js:29:9 在 Layer.handle [as handle_request] (C:\users\rhino\documents\work\personal\polymer-project\node_modules\express\lib\router\layer.js:95:5)

这是我的 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')
});

Picture of Visual Studio Code project folder structure

【问题讨论】:

    标签: javascript json polymer web-component


    【解决方案1】:

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

    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,static和cors中间件,注入中间件如图:

    server.use(middlewares); //logger, static and cors middleware
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-12
      • 2018-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      • 2015-02-19
      • 1970-01-01
      相关资源
      最近更新 更多