带有html5mode的ExpressJS,同时获取API不起作用

时间:2015-11-30 05:58:01

标签: javascript angularjs node.js express

我试图通过将位置提供程序html5mode声明为true来删除使用html5mode的URL上的#符号

$locationProvider.html5Mode(true);

我还配置了我的server.js文件来提供我的静态文件和我的index.html,如下所示

var express = require('express'),
    app = express(),
    mongoose = require('mongoose'),
    bodyParser = require('body-parser'),
    port = process.env.PORT || 8080; 

app.use('/app', express.static(__dirname + '/public/app'));
app.use('/assets', express.static(__dirname + '/public/assets'));
app.use('/libs', express.static(__dirname + '/public/libs'));

app.all('/*', function(req, res, next) {
    res.sendFile('/public/index.html', { root: __dirname });
});

app.use(bodyParser.json()); 

require('./app/routes.js')(app);

app.listen('8080');
console.log('The magic happens on port 8080');

此时,一切正常从localhost:8080到其他导航,例如localhost:8080 / login,即使刷新页面,它仍然正常工作。

在调用任何API fetch语句期间它无法正常工作。下面是我的routes.js

route.js

var mongoose = require('mongoose'),
    db = mongoose.createConnection('mongodb://127.0.0.1/database'),
    Role = require('./models/role.js')(db);
    User = require('./models/user.js')(db);

module.exports = function(app) {

    app.route('/api/user')
        .get(function(req, res, next) {
            User.find(function(err, user){
                 if(err)
                    res.json(err);
                res.json(user);
            });
        })
        .post(function(req, res, next) {
            var error_return = [{response:'User Existed'},{response:'Invalid Username or Password'},{response:'Server Error'}];

            if(req.body.type === "local"){
                var newUser = new User();
                newUser.first_name = req.body.first_name;
                newUser.last_name = req.body.last_name;
                newUser.email = req.body.email;
                newUser.password = newUser.generateHash(req.body.password);
                newUser.type = req.body.type;
                newUser.role = req.body.role;

                User.findOne({email: req.body.email}, function (err, user) {
                    if (err) {
                        res.json(error_return[2]); 
                        return;
                    }
                    if (!user){
                        newUser.save(function(error, result){
                            if(error)
                                res.json(error_return[2]);
                            res.json({response:result});
                        });
                        return;
                    }
                    res.json(error_return[0]);
                });
            }else if(req.body.type === "login"){
                User.findOne({email: req.body.email}, function (err, user) {
                    if (err) {
                        res.json(error_return[2]); 
                        return;
                    }
                    if (!user){
                        res.json(error_return[1]);
                        return;
                    }
                    if (!user.validPassword(req.body.password)){
                        res.json(error_return[1]);
                        return;
                    }
                    res.json({response:user});
                });
            }



        });

};

以前在启用html5mode之前,我的文件在require(' ./ app / routes.js')(app);正在运行,启用后,它甚至没有被调用。我的代码中遗漏了什么吗?

1 个答案:

答案 0 :(得分:0)

您的所有请求现在都在public/index.html

修改代码以允许/api/请求转到路由

app.use('/api', bodyParser.json(), require('./app/routes.js')); 

app.use('/app', express.static(__dirname + '/public/app'));
app.use('/assets', express.static(__dirname + '/public/assets'));
app.use('/libs', express.static(__dirname + '/public/libs'));

app.all('/*', function(req, res, next) {
    res.sendFile('/public/index.html', { root: __dirname });
});

app.listen('8080');
console.log('The magic happens on port 8080');

routes.js

var mongoose = require('mongoose'),
    db = mongoose.createConnection('mongodb://127.0.0.1/database'),
    Role = require('./models/role.js')(db),
    User = require('./models/user.js')(db),
    router = require('express').Router();

module.exports = router;

router.route('/user')
    .get(function (req, res, next) {
    User.find(function (err, user) {
        if (err) res.json(err);
        res.json(user);
    });
})
    .post(function (req, res, next) {
    var error_return = [{
        response: 'User Existed'
    }, {
        response: 'Invalid Username or Password'
    }, {
        response: 'Server Error'
    }];

    if (req.body.type === "local") {
        var newUser = new User();
        newUser.first_name = req.body.first_name;
        newUser.last_name = req.body.last_name;
        newUser.email = req.body.email;
        newUser.password = newUser.generateHash(req.body.password);
        newUser.type = req.body.type;
        newUser.role = req.body.role;

        User.findOne({
            email: req.body.email
        }, function (err, user) {
            if (err) {
                res.json(error_return[2]);
                return;
            }
            if (!user) {
                newUser.save(function (error, result) {
                    if (error) res.json(error_return[2]);
                    res.json({
                        response: result
                    });
                });
                return;
            }
            res.json(error_return[0]);
        });
    } else if (req.body.type === "login") {
        User.findOne({
            email: req.body.email
        }, function (err, user) {
            if (err) {
                res.json(error_return[2]);
                return;
            }
            if (!user) {
                res.json(error_return[1]);
                return;
            }
            if (!user.validPassword(req.body.password)) {
                res.json(error_return[1]);
                return;
            }
            res.json({
                response: user
            });
        });
    }



});