尝试使用Express应用程序生成器向此Express应用程序添加新路由.这是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 routes = require('./routes/index'); var users = require('./routes/users'); var parse = require('./routes/parse'); 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('/',routes); app.use('/users',users); app.use('/parse',parse); // catch 404 and forward to error handler app.use(function(req,res,next) { var err = new Error('Not Found - from app.js'); err.status = 404; next(err); }); // error handlers // development error handler // will print stacktrace if (app.get('env') === 'development') { app.use(function(err,req,next) { res.status(err.status || 500); res.render('error',{ message: err.message,error: err }); }); } // production error handler // no stacktraces leaked to user app.use(function(err,next) { res.status(err.status || 500); res.render('error',{ message: err.message,error: {} }); }); module.exports = app;
var parse = require('./routes/parse'); app.use('/parse',parse);
然后在/ routes目录中我添加了包含以下内容的parse.js:@H_404_5@
var express = require('express'); var router = express.Router(); router.get('/parse',function(req,next) { res.send('return from parse.js'); }); module.exports = router;
我可以使用浏览器点击/和/ users资源,我得到了预期的结果.但是当我尝试点击/解析时,我得到这个404:@H_404_5@
找不到 – 来自app.js@H_404_5@
Error: Not Found - from app.js at /home/bitnami/parsecsvapi/app.js:32:13 at Layer.handle [as handle_request] (/home/bitnami/parsecsvapi/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (/home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:312:13) at /home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:280:7 at Function.process_params (/home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:330:12) at next (/home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:271:10) at /home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:618:15 at next (/home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:256:14) at Function.handle (/home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:176:3) at router (/home/bitnami/parsecsvapi/node_modules/express/lib/router/index.js:46:12)
解决方法
尝试/解析/解析URL
如果你想使用/ parse,请在/routes/parse.js中使用/而不是/ parse@H_404_5@
请参考/routes/index.js> /routes/users.js.@H_404_5@
TL; DR;@H_404_5@
//app.js var parse = require('./routes/parse'); app.use('/parse',parse); var express = require('express'); var router = express.Router(); // routes/parse.js // the URL pattern will be used in conjunction with how its is being used // in this case it would be '/parse' (from app.js) + '/parse' (as used in the next line) // resulting in '/parse/parse' router.get('/parse',next) { res.send('return from parse.js'); }); module.exports = router;