我试图将数据发布到我的Node应用
$.ajax({
dataType: "json",
method: "POST",
url: "/users/new",
data: {
first_name: "Conor",
last_name: "McGregor",
id: 3
},
success: function (data) {
console.log("Success: ", data);
},
error: function (error) {
console.log("Error: ", error);
}
});
。 。 。但我不确定如何从应用程序本身访问帖子数据。
userRouter.route("/new")
.post(function (req, res) {
console.log(req);
//console.log(req.body);
}
经过一个多小时的搜索,我无法找到相关文档。知道怎么做到这一点吗?
编辑:这是我整个申请的入口点。它太乱了 - 抱歉。 : - (
"use strict";
var express = require("express");
var app = express();
var port = process.env.PORT || 5000;
var http = require("http");
var bodyParser = require("body-parser");
var pg = require("pg");
var connectionConfig = {
user: "test_role",
password: "password", // See what happens if you remove this value.
server: "localhost", // I think this would be your database's server and not localhost, normally.
database: "experiment", // The database's name
};
var connectionString = "postgres://test_role:password@localhost/experiment";
var server = http.createServer(function(req, res) {
pg.connect(connectionString, function(error, client, done) {
var handleError = function(error) {
if (!error) {
return false;
}
console.log(error);
if (client) {
done(client);
}
res.writeHead(500, {
"content-type": "text/plain"
});
res.end("An error occurred.");
return true;
};
if (handleError(error)) return;
client.query("INSERT INTO visit (date) VALUES ($1)", [new Date()], function(error, result) {
if (handleError(error)) {
return;
}
client.query("SELECT COUNT(date) AS count FROM visit", function(error, result) {
if (handleError(error)) {
return;
}
done();
res.writeHead(200, {
"content-type": "text-plain"
});
res.end("You are visitor number " + result.rows[0].count);
});
});
});
});
server.listen(3001);
var navigation = [{
link: "/",
text: "Home",
new_window: false
}, {
link: "/users",
text: "Users",
new_window: false
}, {
link: "/contact",
text: "Contact",
new_window: false
}, {
link: "/terms",
text: "Terms",
new_window: false
}];
var userRouter = require("./src/routes/userRoutes")(navigation);
// Routes
app.use("/users", userRouter);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// Serve static files from the public directory
app.use(express.static("public"));
app.use("/public", express.static("public"));
app.set("views", "./src/views");
// Specify EJS as the templating engine
app.set("view engine", "ejs");
app.get("/", function(req, res) {
res.render("index", {
title: "Art is long. Life is short.",
list: ["a", "b", "c", "d"],
navigation: navigation
});
});
app.get("/terms", function(req, res) {
res.render("terms");
});
app.get("/about", function(req, res) {
res.render("about");
});
app.get("/contact", function(req, res) {
res.render("contact");
});
var server = app.listen(port, function() {
var port = server.address().port;
console.log("Listening on port : " + port + " . . .");
});
答案 0 :(得分:1)
您需要一个身体解析器,并在路线前呼叫app.use
:
var bodyparser = require('body-parser'),
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({
extended: true
}));
// Routes
app.use("/users", userRouter);
Express按照声明的顺序将请求传递给每个处理程序,因此必须在userRouter
之前使用正文解析器,否则在调用路由器之前不会解析正文。
答案 1 :(得分:1)
您没有找到任何结果的原因是因为您一直在引用节点本身。为什么不查找特定于特定于文档的文档,因为您仅仅使用节点。
您不仅需要访问req.body
,还必须使用express body parser中间件。
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
// parse application/json
app.use(bodyparser.json());
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({
extended: true
}));
简单快速搜索"表达req.body undefined"或者"表达访问POST数据"会在几分钟内指出你正确的方向,而不是踩几个小时。研究某种解决方案的关键是知道你在寻找什么。同样在你的情况下,尝试理解节点:平台和表达之间的区别:框架。