Express Node JS 404错误


                    

当我尝试使用下面的代码发布ajax请求时,我遇到了404错误” </ p>

  //使用AJAX将对象发布到我们的addReport服务中
     $ .ajax({
         输入:“ POST”,
         数据:newReport,
         网址:“ / add / addreport”,
         dataType:“ JSON”
     })
</ code> </ pre>

我的app.js包含路线</ p>

  var路由= require('./ routes / index');
var reports = require('./ routes / reports');
var add = require('./ routes / add');
</ code> </ pre>

和</ p>

  app.use('/',路线);
app.use('/ reports',报告);
app.use('/ add',添加);
</ code> </ pre>

然后在我拥有的路由文件中</ p>

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

/ *
  *获取reportList。
  * /
router.get('/ reportlist',function(req,res){
     var db = req.db;
     db.collection('reports')。find()。toArray(function(err,items){
         res.json(items);
     });
});

/ *
  * POST添加报告
  * /
router.get('/ addreport',function(req,res){
     var db = req.db;
     db.collection('reports')。insert(req.body,function(err,result){
         重新发送(
             (错误===空) {msg:''}:{msg:err}
         );
     });
});

module.exports =路由器;
</ code> </ pre>

GET请求工作正常。 但是我在POST(添加报告)上得到了404。</ p>

我认为我拥有所有必要的部分,但我是新来表达的人。 也许我缺少什么?</ p>
     </ div>

展开原文

原文

I am struggling with a 404 error when I attempt to post an ajax request using the below code"

// Use AJAX to post the object to our addReport service
    $.ajax({
        type: 'POST',
        data: newReport,
        url: '/add/addreport',
        dataType: 'JSON'
    })

My app.js contains the routes

var routes = require('./routes/index');
var reports = require('./routes/reports');
var add = require('./routes/add');

and

app.use('/', routes);
app.use('/reports', reports);
app.use('/add', add);

and then in the route file I have

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

/*
 * GET reportList.
 */
router.get('/reportlist', function(req, res) {
    var db = req.db;
    db.collection('reports').find().toArray(function (err, items) {
        res.json(items);
    });
});


/*
 * POST to addreport
 */
router.get('/addreport', function(req, res) {
    var db = req.db;
    db.collection('reports').insert(req.body, function(err, result){
        res.send(
            (err === null) ? { msg: '' } : { msg: err }
        );
    });
});

module.exports = router;

The GET request works fine. But I get a 404 on the POST (addreport).

I think I have all the necessary parts but I am new to express. Perhaps I am missing something?

1个回答


对于“ addreport”,您还应该在使用get的路线上更改方法</ p>

尝试将其更改为像这样发布:</ p>

  router.post('/ addreport',function(req,res){
     var db = req.db;
     db.collection('reports')。insert(req.body,function(err,result){
         重新发送(
             (错误===空) {msg:''}:{msg:err}
         );
     });
});
</ code> </ pre>
     </ div>

展开原文

原文

you should change your method on the route you are using get as well for 'addreport'

try to change it to post like this:

router.post('/addreport', function(req, res) {
    var db = req.db;
    db.collection('reports').insert(req.body, function(err, result){
        res.send(
            (err === null) ? { msg: '' } : { msg: err }
        );
    });
});

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐