gpt4 book ai didi

javascript - Node Js 路由不起作用

转载 作者:行者123 更新时间:2023-12-02 14:39:15 25 4
gpt4 key购买 nike

我正在尝试在nodejs中创建一个api。

我的 server.js 文件如下

var http = require('http'),
path = require('path'),
async = require('async'),
socketio = require('socket.io'),
express = require('express'),
session = require('express-session'),
logger = require('morgan'),
bodyParser = require('body-parser');

var app = express();
var server = http.createServer(app);
var io = socketio.listen(server);
app.use(logger('dev'));
app.use(bodyParser.json());

app.all('/*', function(req, res, next) {
// CORS headers
res.header("Access-Control-Allow-Origin", "*"); // restrict it to the required domain
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
// Set custom headers for CORS
res.header('Access-Control-Allow-Headers', 'Content-type,Accept,X-Access-Token,X-Key');
if (req.method == 'OPTIONS') {
res.status(200).end();
} else {
next();
}
});
app.all('/api/*', [require('./middlewares/validateRequest')]);
//app.use(express.static(path.resolve(__dirname, 'client')));

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

// If no route is matched by now, it must be a 404
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

var messages = [];
var sockets = [];

......
server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function(){
var addr = server.address();
console.log("Meeting Panel listening at", addr.address + ":" + addr.port);
});

我的/routes目录有index.js文件

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

var auth = require('./auth.js');
var meetings = require('./meetings.js');
var user = require('./users.js');

/*
* Routes that can be accessed by any one
*/
router.post( '/login', auth.login);


/*
* Routes that can be accessed only by authenticated & authorized users
*/
router.get('/api/admin/users', user.getAll);
.....
.....

module.exports = router;

现在,当我打开/login 页面时,我收到以下错误

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

最佳答案

您在 /login 上的路由是在 POST 方法中定义的。如果您想从浏览器打开它(我认为您正在做的事情),您应该定义 router.get( '/login'... 。我猜您不想在 GET 方法上定义了 /login 路由,因此另一个不错的测试选择是点击 /api/admin/users

关于javascript - Node Js 路由不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37161956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com