POST http:// localhost:3000 / api / signup / 404(未找到)

时间:2019-07-05 12:12:10

标签: node.js reactjs express sequelize.js

我正在尝试发送用户的数据以进行表达并使用create函数创建新用户 但显示错误404和找不到页面 为什么?

我的api端点(可在client / src / components / api-users中找到:

real_result = []
for key in result:
    real_result.append((key, dict.get(key))

我用于注册的用户控制器:

async function create (data) {
  try {

    const resp = await fetch('/api/signup/' , { //error initiating here
        method:'POST',
        mode:"cors",
        credentials:'include',
        headers:{
          'Content-Type': 'application/json ',
          'Accept': 'application/json',
          "Access-Control-Origin": "*"
        },
        body:JSON.stringify(data)
      })
    console.log(resp)

    console.log(resp.body)
    resp.headers.forEach(console.log);

    return JSON.stringify(resp);
  } catch (err) {
       console.log(err)
    }
}



    export {
      create

    }

用于数据库连接的服务器js文件:

var jwt = require('jsonwebtoken');
var atob =require('atob')
var Cryptr = require('cryptr')
var cryptr = new Cryptr('q1w2e3r4t5y6u7i8o9p0p0o9i8u6y5t4r3e2w1q')
var db  =require('../server')
const create = (req, res, next) =>{      
     first_name  = req.body.first_name,
     last_name = req.body.last_name,
     username =req.body.username,
     password= req.body.password,
     email=req.body.email,
     dec_pass =atob(toString(req.body.password)),
     encrypted_pass =cryptr.encrypt(dec_pass)

    var sql = "INSERT INTO `user`(`user_id`,`first_name`,`last_name`,`username` , `email`,`password`) VALUES ('','" + first_name + "','" + last_name + "','" + username + "','" +email+ "','" +encrypted_pass+ "')";
    var query = db.query(sql, function(err, result){
       console.log(query)
      console.log(req)
       return (JSON.stringify(result));
    });
};

  export 
  { create }

亩用户路线:

var  Sequelize = require('sequelize')
var app = require('./app')
var CONFIG= require('../config/CONFIG')
const db = {}


const sequelize = new Sequelize ("users" , "root" , "" ,{

        host:'localhost',
        dialect:'mysql',
        operatorAliases:false,

        pool:{
            max:5,
            min:0,
            acquire:30000,
            idle:10000
        }

})

console.log(CONFIG.db_host)
db.sequelize=sequelize
db.Sequelize=Sequelize
console.log('alright')
export default db

我的signupjs反应文件

const express           = require('express');
const router            = express.Router();
var userCtrl = require ('../controllers/user.controller')
router.post('/signup', userCtrl.create) 
module.exports = router

2 个答案:

答案 0 :(得分:0)

正如@Deep Kakkar所述,您没有设置api前缀,因此您应该找到/signup而不是/api/signup

fetch(/api/signup)还会在您当前域(响应应用程序已打开的地方)上到达此相对路径,因此您需要设置完整路径,例如,如果您的http-server在端口{ {1}},而您的应用程序已在4000上运行,那么您应该获取3000而不是http://localhost:4000/api/signup,因为它将是/api/signup

答案 1 :(得分:0)

本地主机:3000 / api / products 404错误 您没有在server.js上创建res.get(“ / api / products”)或未设置代理。请在下面检查代理设置。

代理错误:无法代理请求/ api / products 检查一下:

  1. frontend / package.json

    { “ name”:“ frontend”, “ proxy”:“ http://127.0.0.1:5000”, ... }

  2. 停止运行前端和后端

  3. 先运行后端

    npm开始

  4. 然后是前端

    cd前端 npm开始