Node.js和express:路由 [英] Node.js and express : Routes

查看:112
本文介绍了Node.js和express:路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Express路由器时遇到一些麻烦。
我想用几个文件设置路线。
我的路线文件夹包含2个文件:route.js和inscription.js

I have some trouble using the router from Express. I want to set up my routes with several files. I get my routes folder with 2 files: routes.js and inscription.js

我执行以下操作

var inscription = require('./routes/inscription.js');
var routes = require('./routes/routes.js');

然后

app.use('/', routes);
app.use('/inscription', inscription);

但是只有来自route.js的路由才起作用...

But only the routes from routes.js work...

这是routes.js的内容

This is the content of routes.js

var router = require('express').Router();
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false});

//Homepage
router.get('/', function(req, res){
    res.setHeader('Content-Type', 'text/html');
    res.status(200);
    res.render('home.ejs');
});

//Connexion
router.post('/connexion', urlencodedParser, function(req, res){
    //Some content
});
module.exports = router;

这是inscription.js的内容

And this is the content of inscription.js

var router = require('express').Router();
var hash = require('password-hash');
var db = require('../models/users.js');
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false});

router.get('/inscription', function(req, res){
    res.setHeader('Content-Type', 'text/html');
    res.status(200);
    res.render('inscription.ejs');
});
router.post('/adduser', urlencodedParser, function(req, res){
    var passwordHashed = hash.generate(req.body.inputPassword);
    var newUser = {
        nom      : req.body.inputName,
        email     : req.body.inputEmail,
        password : passwordHashed
    };
    db.addUser(newUser);
    res.redirect('/');
});
router.post('/checkname', urlencodedParser, function(req, res){
    var user = {
        nom      : req.body.inputName
    };
    db.checkName(user, function(length){
        res.send(length);
    });
});
router.post('/checkemail', urlencodedParser, function(req, res){
    var user = {
        email      : req.body.inputEmail
    };
    db.checkEmail(user, function(length){
        res.send(length);
    });
});
module.exports = router;

inscription.js的内容在粘贴到routes.js文件中时起作用...
所以我想这就是我无法导入文件的方式。

The content of inscription.js works when it is pasted in the routes.js file ... So I guess it is how I import the file that is not working.

有什么想法吗?

推荐答案

题词路由器中的此路由 router.get('/ inscription',...)已配置为路由 /题词/题词,这可能不是您想要的。这是因为您在两个地方指定了它:

This route router.get('/inscription', ...) in your inscription router is configured for the route /inscription/inscription which is likely not what you intended. This is because you've specified it in two places:

app.use('/inscription', inscription);
router.get('/inscription', ...)

所以,整个路由器位于 app.use('/ inscription',inscription) / inscription 上。这意味着路由器本身定义的任何路由都将被添加到该路径。

So, the whole router is on /inscription from the app.use('/inscription', inscription). That means that any route the router itself defines will be added to that path.

从您的问题中并不清楚您到底是什么打算将URL作为。但是,如果您只是想让上述 router.get()用于 / inscription URL,请更改:

It isn't exactly clear from your question exactly what you intend for the URLs to be. But, if you just want the above router.get() to work for a /inscription URL, then change:

router.get('/inscription', ...)

至:

router.get('/', ...)

当您使用 app.use('/ inscription' ,题字); ,该路由器中的每条路由都将以 /题字作为前缀。因此,这条路线:

When you use app.use('/inscription', inscription);, every single route in that router will be prefixed with /inscription. So, this route:

router.post('/adduser', ...)

将安装在以下位置:

/inscription/adduser






或者,如果您要全部题词路由也要处于最高级别,然后更改:


Or, if you want all the inscription routes to be at the top level too, then change:

app.use('/inscription', inscription);

为此:

app.use('/', inscription);

因此,除了路由器自己定义的路径之外,没有其他内容添加到路径中。

So that nothing is added to the path beyond what the router itself defines.

这篇关于Node.js和express:路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆