app.get() 方法似乎没有在 Express 应用程序中被调用

我是 MEAN 堆栈的新手,正在尝试实现 express。出于某种原因,当导航到我已设置要处理的应用程序对象的路径时,我收到 404。我知道该应用程序必须正在运行,因为该错误正在由我放入的用于处理 404 的自定义方法处理。

http://img3.mukewang.com/63a3b4300001890806380216.jpg

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...');

}

  )


幕布斯7119047
浏览 82回答 1
1回答

aluckdog

app.get('/test',function (req, res) { ... your code ...}) 类别相同...您忘记了 URL 开头的“/ ”
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript