[英]Express route not working
I am using express as server and in the last few days i tried to make a new routes under the "localhost:3000/app/home" path. 我使用express作为服务器,最近几天我尝试在“ localhost:3000 / app / home”路径下创建一条新路由。 The problem is that i receive a 404 error from the server when i open this path.
问题是当我打开此路径时,我从服务器收到404错误。 i am sure that the file exist, so i think that the problem is the route configuration of express.
我确定该文件存在,所以我认为问题是Express的路由配置。
App.js 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 session = require('express-session');
var index = require('./routes/index');
var app_index = require('./routes/app');
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: true }));
app.use(cookieParser());
app.use(session({ secret: 'ashasjasdiw', resave: true, saveUninitialized: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/app', app_index);
// 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');
});
app.listen(3000);
module.exports = app;
this is my route App.js 这是我的路线App.js
var express = require('express');
var router = express.Router();
router.get('/app', function(req, res){
res.render('app/home');
});
module.exports = router;
I use a redirect to go under the path "/app/home" and i don't know if it's the best way to do that. 我使用重定向进入路径“ / app / home”,但我不知道这是否是最好的方法。 this is the error page:
这是错误页面:
I think you might need to remove the app
from the route in app.js
我认为您可能需要从
app.js
的路由中删除该app
var express = require('express');
var router = express.Router();
router.get('/', function(req, res){
res.render('app/home');
});
module.exports = router;
You are mounting the route /app
on the application's /app
. 您正在将路由
/app
挂载到/app
程序的/app
。 The resultant route is therefore /app/app
not /app/home
. 因此,结果路由为
/app/app
而不是/app/home
。 If you wish to access the route from /app/home
you will need to change the route to /home
. 如果您希望从
/app/home
访问路由,则需要将路由更改为/home
。
router.get('/home', function(req, res){
I had the same problem. 我有同样的问题。 I finally realized that in your app.js file (main file) you use
app.use('/api','./routes/index');
我终于意识到,在您的app.js文件(主文件)中,您使用了
app.use('/api','./routes/index');
. 。 When you go into your index.js file, your
router.get
, router.post
, router.put
etc. need to be router.get('/', function(...))
which will refer to your http://localhost/api
! 当您进入index.js文件时,您的
router.get
, router.post
, router.put
等需要为router.get('/', function(...))
,它将引用您的http://localhost/api
!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.