2017-02-11 65 views
0

我想我的登錄路由從默認app.jsroute/index.js文件分開,但我沒有得到error 404在執行localhost:3000/login無法在快速JS設置單獨的航線文件

找不到我知道這個問題之前,已經被問我在StackOverflow上跟着其他答案,但沒有得到爲什麼我得到錯誤。

堆棧溢出的問題我也跟着: How to separate routes on Node.js and Express 4?

我沒有變,爲什麼我得到這個錯誤。

app.js

var express = require('express'); 
var path = require('path'); 
var favicon = require('serve-favicon'); 
var logger = require('morgan'); 
var cookieParser = require('cookie-parser'); 
var bodyParser = require('body-parser'); 
var expressValidator = require('express-validator'); 
var session = require('express-session'); 

var mongodb = require('./mongoDB'); 
var index = require('./routes/index'); 
var users = require('./routes/users'); 
var login = require('./routes/login'); //including login.js 

var app = express(); 

// view engine setup 
app.set('views', path.join(__dirname, 'views')); 
app.set('view engine', 'jade'); 

// uncomment after placing your favicon in /public 
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); 
app.use(logger('dev')); 
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: false })); 
app.use(cookieParser()); 
app.use(express.static(path.join(__dirname, '/public'))); 
    app.use(session({ 
    name: 'mysession', 
    secret: 'some_secret', 
    saveUninitialized: false, 
    resave: true, cookie: { 
     secure: true 
    }})); 

app.use('/', index); 
app.use('/users', users); 
app.use('/login', login); 

mongodb.connectDatabase; 

// catch 404 and forward to error handler 
app.use(function(req, res, next) { 
    var err = new Error('Not Found'); 
    err.status = 404; 
    next(err); 
}); 

// error handler 
app.use(function(err, req, res, next) { 
    // set locals, only providing error in development 
    res.locals.message = err.message; 
    res.locals.error = req.app.get('env') === 'development' ? err : {}; 

    // render the error page 
    res.status(err.status || 500); 
    res.render('error'); 
}); 

module.exports = app; 

路線/ login.js

var express = require('express'); 
var router = express.Router(); 

router.get('/login', function (req, res) { 
    res.send('exec'); 
}); 

module.exports = router; 

本地主機:3000 /登錄

Not Found 
404 
Error: Not Found 
    at /home/jatin/Drive/OpenSource/sessionManager/app.js:44:13 
    at Layer.handle [as handle_request] (/home/jatin/Drive/OpenSource/sessionManager/node_modules/express/lib/router/layer.js:95:5) 
    at trim_prefix (/home/jatin/Drive/OpenSource/sessionManager/node_modules/express/lib/router/index.js:312:13) 
    at /home/jatin/Drive/OpenSource/sessionManager/node_modules/express/lib/router/index.js:280:7 
    at Function.process_params (/home/jatin/Drive/OpenSource/sessionManager/node_modules/express/lib/router/index.js:330:12) 
    at next (/home/jatin/Drive/OpenSource/sessionManager/node_modules/express/lib/router/index.js:271:10) 
    at /home/jatin/Drive/OpenSource/sessionManager/node_modules/express/lib/router/index.js:618:15 
    at next (/home/jatin/Drive/OpenSource/sessionManager/node_modules/express/lib/router/index.js:256:14) 
    at Function.handle (/home/jatin/Drive/OpenSource/sessionManager/node_modules/express/lib/router/index.js:176:3) 
    at router (/home/jatin/Drive/OpenSource/sessionManager/node_modules/express/lib/router/index.js:46:12) 
    at Layer.handle [as handle_request] (/home/jatin/Drive/OpenSource/sessionManager/node_modules/express/lib/router/layer.js:95:5) 
    at trim_prefix (/home/jatin/Drive/OpenSource/sessionManager/node_modules/express/lib/router/index.js:312:13) 
    at /home/jatin/Drive/OpenSource/sessionManager/node_modules/express/lib/router/index.js:280:7 
    at Function.process_params (/home/jatin/Drive/OpenSource/sessionManager/node_modules/express/lib/router/index.js:330:12) 
    at next (/home/jatin/Drive/OpenSource/sessionManager/node_modules/express/lib/router/index.js:271:10) 
    at /home/jatin/Drive/OpenSource/sessionManager/node_modules/express/lib/router/index.js:618:15 

回答

1

這是一個有點confus荷蘭國際集團,但是當你使用:

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

您嵌套/login命名空間下login定義的所有路線。在/login/login

你可能目前訪問路線

更改login.js到:

var express = require('express'); 
var router = express.Router(); 

router.get('/', function (req, res) { 
    res.send('exec'); 
}); 

module.exports = router; 

然後/login/將正確地路由到login命名空間

+0

感謝@Rob M.呼救/路線 –