Node.js 快速服务器接收POST请求 下面是一个特快节目
问题:: 我在客户端中没有得到正确的JSON响应 我想知道的::Node.js 快速服务器接收POST请求 下面是一个特快节目,node.js,express,Node.js,Express,问题:: 我在客户端中没有得到正确的JSON响应 我想知道的:: 当我给出一个示例时,我是否在服务器(Express)中编写了正确的代码 来自客户端的post请求 客户端以(键、值)对的形式发送请求 我确信得到我面临的问题的请求结构 我如何解决它? 有什么想法吗?您没有处理POST请求的路由。你应该有类似于app.post('/route',函数(request,response,next){…}的东西 您还需要使用Express body解析器从POST请求中获取数据,即app.use
- 当我给出一个示例时,我是否在服务器(Express)中编写了正确的代码 来自客户端的post请求
- 客户端以
对的形式发送请求(键、值)
- 我确信得到我面临的问题的请求结构
有什么想法吗?您没有处理POST请求的路由。你应该有类似于app.post('/route',函数(request,response,next){…}的东西代码>
您还需要使用Express body解析器从POST请求中获取数据,即
app.use(Express.bodyParser())代码>。然后,您可以在中间件功能中访问request.body中的数据。您没有处理POST请求的路由。你应该有类似于app.post('/route',函数(request,response,next){…}的东西代码>
您还需要使用Express body解析器从POST请求中获取数据,即app.use(Express.bodyParser())代码>。然后,您可以访问中间件功能中的请求.body
中的数据。启用Express日志记录以帮助调试也可能很有用,即应用程序使用(Express.logger())
现在您需要单独安装body parser,像这样要求它var bodyParser=require('body-parser')
并像这样使用它app.use(bodyParser())
启用Express的日志记录以帮助调试也可能很有用,例如app.use(Express.logger())
现在您需要单独安装npm body parser,像这样要求它var bodyParser=require('body-parser')
并像这样使用它app.use(bodyParser())
var express = require('express')
, async = require('async')
, http = require('http')
, mysql = require('mysql');
var app = express();
var connection = mysql.createConnection({
host: 'localhost',
user: '*************',
password: "**************",
database: 'restaurants'
});
connection.connect();
// all environments
app.set('port', process.env.PORT || 1828);
app.get('/RestaurantDesc/:Key',function(request,response,next){
var keyName=request.params.Key;
var name_of_restaurants, RestaurantTimings;
async.series( [
// Get the first table contents
function ( callback ) {
connection.query('SELECT * FROM ',keyName, function(err, rows, fields)
{
console.log('Connection result error '+err);
name_of_restaurants = rows;
callback();
});
},
// Get the second table contents
function ( callback ) {
connection.query('SELECT * FROM RestaurantTimings', function(err, rows, fields)
{
console.log('Connection result error '+err);
RestaurantTimings = rows;
callback();
});
}
// Send the response
], function ( error, results ) {
response.json({
'restaurants' : name_of_restaurants,
'RestaurantTimings' : RestaurantTimings
});
} );
} );
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});