im试图使用node.js设置API,并在我的app.js类中处理请求错误,以防万一出问题我返回404,这就是我的问题,我看不到我如何请求任何东西错误,我仍然收到404错误,即时通讯正试图像这样向我的API发送发布请求:
{
"name":"Harry Potter 5",
"price":"12.99"
}
那我明白了
这是我的app.js
const express = require('express');
const app = express();
const morgan = require('morgan');
const productRoutes = require('./api/routes/product');
const orderRoutes = require('./api/routes/order');
const bodyParser = require('body-parser');
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({
extended:false
}));
app.use(bodyParser.json());
app.use((req, res, next) => {
const error = new Error("Not found");
error.status = 404;
next(error);
});
app.use((error, req, res, next) => {
res.status(error.status || 500);
res.json({
error: {
message: error.message
}
});
});
app.use('/products', productRoutes);
app.use('/orders', orderRoutes);
module.exports = app;
这是我的product.js
const express = require('express');
const router = express.Router();
router.get('/', (req, res, next) => {
res.status(200).json({
message: 'Handling GET requests to /products'
});
});
router.post('/', (req, res, next) => {
const product = {
name: req.body.name,
price: req.body.price
};
res.status(201).json({
message: 'Handling POST requests to /products',
createdProduct: product
});
});
router.get('/:productId', (req, res, next) => {
const id = req.params.productId;
if (id === 'special') {
res.status(200).json({
message: 'You discovered the special ID',
id: id
});
} else {
res.status(200).json({
message: 'You passed an ID'
});
}
});
router.patch('/:productId', (req, res, next) => {
res.status(200).json({
message: 'Updated product!'
});
});
router.delete('/:productId', (req, res, next) => {
res.status(200).json({
message: 'Deleted product!'
});
});
module.exports = router;
答案 0 :(得分:1)
这是因为您正在将所有错误设置为:)
请参阅here中的文档-通过提供的链接:
编写错误处理程序在中定义错误处理中间件功能 除错误处理外,与其他中间件功能相同 函数具有四个参数,而不是三个:(err,req,res,next)。 例如:
// pay attention to err param app.use(function (err, req, res, next) { console.error(err.stack)` res.status(500).send('Something broke!') })
在您的代码中,您具有以下功能:
app.use((req, res, next) => {
const error = new Error("Not found");
error.status = 404;
next(error);
});
告诉我们,每个请求都应以404响应。您应该将其设置为适当的错误处理程序,或将其删除。
答案 1 :(得分:0)
这是因为任何请求都会执行404处理程序。
看一下这段简短的代码:
for kk in range(1,10):
# ---- set initial conditions (random)
decisionE.gen = decisionE.gen * (1 + 0.2 * rand(decisionE.__len__()))
decisionI.gen = decisionI.gen * (1 + 0.2 * rand(decisionI.__len__()))
decisionE.V = decisionE.V + rand(decisionE.__len__()) * 2 * mV
decisionI.V = decisionI.V + rand(decisionI.__len__()) * 2 * mV
# ---- set initial conditions (random)
sensoryE.V = -50.0 * mV - 2 * mV + rand(sensoryE.__len__()) * 2 * mV
sensoryI.V = -50.0 * mV - 2 * mV + rand(sensoryI.__len__()) * 2 * mV
sensoryE.gea = 0.05 * (1 + rand(sensoryE.__len__()) * 0.2)
sensoryI.gea = 0.05 * (1 + rand(sensoryI.__len__()) * 0.2)
# record spikes of excitatory neurons
S_DE1 = SpikeMonitor(decisionE1, record=True)
S_DE2 = SpikeMonitor(decisionE2, record=True)
S_SE1 = SpikeMonitor(sensoryE1, record=True)
S_SE2 = SpikeMonitor(sensoryE2, record=True)
# record instantaneous populations activity
R_DE1 = PopulationRateMonitor(decisionE1, bin=20 * ms)
R_DE2 = PopulationRateMonitor(decisionE2, bin=20 * ms)
R_SE1 = PopulationRateMonitor(sensoryE1, bin=20 * ms)
R_SE2 = PopulationRateMonitor(sensoryE2, bin=20 * ms)
print kk
# ------------------------------------------------------------------------------
# Run the simulation
# ------------------------------------------------------------------------------
# construct network
net = Network(Dgroups.values(), Sgroups.values(), Dconnections.values(), Sconnections.values(),
Dnetfunctions, update_input, C_SE1_DE1, C_SE2_DE2, C_DE1_SE1, C_DE2_SE2,
S_DE1, S_DE2, S_SE1, S_SE2, R_DE1, R_DE2, R_SE1, R_SE2)
net.prepare()
net.run(runtime)
ck_=int(runtime/20)-1
for ii in range(1,ck_):
if (R_DE1.rate[ii]>=15):
break
for jj in range(1,ck_):
if (R_DE2.rate[jj]>=15):
break
if (jj>=ii):
print R_DE1.rate[ii]
print ("A fired at ")
print R_DE1.times[ii]
else:
print R_DE2.rate[jj]
print("B fired at ")
print R_DE2.times[jj]
在每个请求时打印“进入404处理程序”。现在,如果您以这种方式注释掉404回调:所有请求都会通过500和200回调:
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended:false
}));
app.use(bodyParser.json());
app.use((req, res, next) => {
console.log("Got into 404 handler");
const error = new Error("Not found");
error.status = 404;
next(error);
});
app.use((error, req, res, next) => {
console.log("Got into 500 handler");
res.status(error.status || 500);
res.json({
error: {
message: error.message
}
});
});
app.use('/products', (req, res, next) => {
console.log("Got into 200 handler");
res.status(200).end();
});
app.listen(8080);
现在在您的特定问题中,下面的代码将起作用(我只是交换了处理程序的顺序):
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended:false
}));
app.use(bodyParser.json());
/* there used to be the 404 callback here */
app.use((error, req, res, next) => {
console.log("Got into 500 handler");
res.status(error.status || 500);
res.json({
error: {
message: error.message
}
});
});
app.use('/products', (req, res, next) => {
console.log("Got into 200 handler");
res.status(200).end();
});
app.listen(8080);
希望这会有所帮助。