最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

快速路由注册Ajax返回POST http: localhost:3004signup404

IT培训 admin 6浏览 0评论

快速路由注册Ajax返回POST http:// localhost:3004 / signup / 404

当我向我的快递http://localhost:3004/signup/路线提交一个Ajax帖子时,我得到了一个POST /signup/ 404。任何帮助将不胜感激。我是节点的新手!

server.js

const pg = require('pg')
const bodyParser = require('body-parser')
const knex = require('./knex')
const express =require('express')
var path = require('path');
const router = express.Router()
const port = process.env.PORT || 3004
const app = express()

app.use(express.static(path.join(__dirname, 'public')));

router.get('/', (req, res, next) => {
    res.status(200).send('working')
})

router.get('/user', (req, res, next) => {
    knex('user').then(data=>{
        res.status(200).send(data)
    })
})

router.post('/signup/', (req, res, next) => {
    knex('user').insert({
        name:req.body.name,
        lastName:req.body.nameLast,
        email:req.body.email,
        password:req.body.password
    },'*')
    .then(user=>{
        res.status(204).send({id:user[0].id})
    })
})





app.use((err, req, res, next) => {
    const status = err.status || 404
    res.status(status).json({ error: err })
})

app.use((req, res, next) => {
    res.status(404).json({ error: { status: 404, message: 'Not found' 
    }})
})

const listener = () => `Listening on port ${port}!`
app.listen(port, listener)

signup.js

$( document ).ready(function() {
     const baseURL = 'http://localhost:3004'
     $( "#submit" ).on( "click", function() {
        event.preventDefault();
        const name = $( "#name" ).val();
        const last = $( "#nameLast" ).val()
        const email = $( "#email" ).val()
        const password = $( "#password" ).val()

        $.ajax({
            url: `/signup/`,
            type: 'POST',
            data: {
              name: name,
              last: last,
              email: email,
              password: password
            },
            success: function (data) {
                window.location.href = '/index.html'
            }
        })
    })
});
回答如下:

你必须注册路由器

app.use('/', router);

你可以在上面添加它

app.use((err, req, res, next) => {
    const status = err.status || 404
    res.status(status).json({ error: err })
})

例如。它在官方documentation(最后一节)中解释。


为了能够读取req.body,您必须启用正文解析器中间件。加

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

就在下面

app.use(express.static(path.join(__dirname, 'public')));

确保在定义路径之前添加行。当您添加这两行时,body-parser会解析JSON和x-www-form-urlencoded内容。您正在ajax请求中发送x-www-form-urlencoded。

快速路由注册Ajax返回POST http:// localhost:3004 / signup / 404

当我向我的快递http://localhost:3004/signup/路线提交一个Ajax帖子时,我得到了一个POST /signup/ 404。任何帮助将不胜感激。我是节点的新手!

server.js

const pg = require('pg')
const bodyParser = require('body-parser')
const knex = require('./knex')
const express =require('express')
var path = require('path');
const router = express.Router()
const port = process.env.PORT || 3004
const app = express()

app.use(express.static(path.join(__dirname, 'public')));

router.get('/', (req, res, next) => {
    res.status(200).send('working')
})

router.get('/user', (req, res, next) => {
    knex('user').then(data=>{
        res.status(200).send(data)
    })
})

router.post('/signup/', (req, res, next) => {
    knex('user').insert({
        name:req.body.name,
        lastName:req.body.nameLast,
        email:req.body.email,
        password:req.body.password
    },'*')
    .then(user=>{
        res.status(204).send({id:user[0].id})
    })
})





app.use((err, req, res, next) => {
    const status = err.status || 404
    res.status(status).json({ error: err })
})

app.use((req, res, next) => {
    res.status(404).json({ error: { status: 404, message: 'Not found' 
    }})
})

const listener = () => `Listening on port ${port}!`
app.listen(port, listener)

signup.js

$( document ).ready(function() {
     const baseURL = 'http://localhost:3004'
     $( "#submit" ).on( "click", function() {
        event.preventDefault();
        const name = $( "#name" ).val();
        const last = $( "#nameLast" ).val()
        const email = $( "#email" ).val()
        const password = $( "#password" ).val()

        $.ajax({
            url: `/signup/`,
            type: 'POST',
            data: {
              name: name,
              last: last,
              email: email,
              password: password
            },
            success: function (data) {
                window.location.href = '/index.html'
            }
        })
    })
});
回答如下:

你必须注册路由器

app.use('/', router);

你可以在上面添加它

app.use((err, req, res, next) => {
    const status = err.status || 404
    res.status(status).json({ error: err })
})

例如。它在官方documentation(最后一节)中解释。


为了能够读取req.body,您必须启用正文解析器中间件。加

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

就在下面

app.use(express.static(path.join(__dirname, 'public')));

确保在定义路径之前添加行。当您添加这两行时,body-parser会解析JSON和x-www-form-urlencoded内容。您正在ajax请求中发送x-www-form-urlencoded。

发布评论

评论列表 (0)

  1. 暂无评论