weixin_33747129 2017-07-09 07:17 采纳率: 0%
浏览 58

无法发布ajax请求

can anyone explain to me why i cannot post the ajax request. When i run this code the console appear POST http://localhost:8080/api/users 404 (Not Found), and in the networth part the preview is 404 not found In the index.js file

        var path = require('path');
    var express = require('express');
    var webpack = require('webpack');
    var config = require('./webpack.config.dev.js');

    var app = express();
    var compiler = webpack(config);
    var bodyParser = require('body-parser');
    var users = require('./server/routes/users');

    app.use(bodyParser.json());

    app.use('/api/users', users);

    app.use(require('webpack-dev-middleware')(compiler, {
      noInfo: true,
      publicPath: config.output.publicPath
    }));

    app.use(require('webpack-hot-middleware')(compiler));

    app.get('*', function(req, res) {
      res.sendFile(path.join(__dirname, 'index.html'));
    });

    app.listen(8080, 'localhost', function(err) {
      if (err) {
        console.log(err);
        return;
      }

      console.log('Listening at http://localhost:8080');
    });

and the users file

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

    // middleware that is specific to this router
    function validateInput(data) {
      let errors = {};

      if (Validator.isNull(data.username)){
        errors.username = "This field is required";
      }
      if (Validator.isEmail(data.email)) {
        errors.email = "Email is invalid";
      }
      if (Validator.isNull(data.password)){
        errors.password = 'This field is required';
      }
      if (Validator.isNull(data.passwordConfirmation)){
        errors.passwordConfirmation = 'This field is required';
      }
      if (Validator.equals(data.password, data.passwordConfirmation)){
        errors.passwordConfirmation = 'Password must match';
      }
      return {
        errors,
        isValid: isEmpty(errors)
      }
    }

    router.post('/api/users', (req, res) => {

          console.log('runiing the router/post');
          console.log(req.body);
          const {errors, isValid} = validateInput(req.body);

          if (!isValid) {
            res.status(400).json(errors);
          }

    });

    module.exports = router
  • 写回答

1条回答 默认 最新

  • 妄徒之命 2017-07-09 07:23
    关注

    in users.js file the function should be

    router.post('/', (req, res) => {
    
              console.log('runiing the router/post');
              console.log(req.body);
              const {errors, isValid} = validateInput(req.body);
    
              if (!isValid) {
                res.status(400).json(errors);
              }
    
        });
    

    Because index.js have already resolved /api/users part in the request url http://localhost:8080/api/users at this point. So you only have to map after the /api/users in your users js file.

    For example, if you have following function in users.js file

    router.post('/:id', (req, res) => {
          ...
        }
    

    It will resole to the path http://localhost:8080/api/users/1

    Edit In your existing version,

    router.post('/api/users', (req, res) => {
      ...
    }
    

    will resolve as http://localhost:8080/api/users/api/users

    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?