我是 MEAN 堆栈的新手,正在尝试实现 express。出于某种原因,当导航到我已设置要处理的应用程序对象的路径时,我收到 404。我知道该应用程序必须正在运行,因为该错误正在由我放入的用于处理 404 的自定义方法处理。var express = require('express');var path = require('path');var bodyParser = require('body-parser');var mongo = require('mongoose');var db = mongo.connect("mongodb://localhost:27017/recipedb", function (err, response) { if (err) { console.log(err); } else { console.log('Connected to ' + db, ' + ', response) }});var app = express();app.use(bodyParser());app.use(bodyParser.json({ limit: 'Smb' }));app.use(bodyParser.urlencoded({ extended: true }));var Schema = mongo.Schema;var categorySchema = new Schema({ id: { type: String }, name: { type: String }}, {versionKey: false});var cModel = mongo.model('category', categorySchema, 'category');app.use(function (req, res, next) { res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200'); res.setHeader('Access-Control-Allow-Methods', 'GET','POST','OPTIONS','PUT','PATCH','DELETE',); res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); res.setHeader('Access-Control-Allow-Credentials', true); next();});app.get("test", function (req, res) { res.send('The GET Request was handled by express'); })app.get("category", function (req, res) { model.find({}, function (err, data) { if (err) { res.send(err); } else { res.send(data); } });}) app.use(function (req, res, next) { res.status(404).send('Unable to find the requested resource!');});app.listen(3000, function () { console.log('Rapid Recipes listening on port 3000...');} )
1 回答
aluckdog
TA贡献1847条经验 获得超7个赞
app.get('/test',function (req, res) { ... your code ...})
类别相同...您忘记了 URL 开头的“/ ”
添加回答
举报
0/150
提交
取消