在Express 4中查询字符串变量仍然为空 [英] Query string variables still empty in Express 4

查看:52
本文介绍了在Express 4中查询字符串变量仍然为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照此处 stackoverflow 上的建议编写简单服务器:

Write simple server as suggested here on stackoverflow:

rest_api.js:

const express = require('express');
const bodyParser = require('body-parser')

// Initialize RESTful server
const app = express();

app.set('port', (process.env.PORT || 5000));
app.use(bodyParser.json() );                        // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({extended: true}));   // to support URL-encoded bodies

// Spin up the server
app.listen(app.get('port'), function() {
    console.log('running on port', app.get('port'))
});

app.post('/bookinghandler', function (req, res) {
    let senderId = req.query.sender;
    let email = req.query.email;
    let hotelName = req.query.hotelName;
    let confirmNumber = req.query.confirmNumber;
    let start = req.query.startDate;
    let end = req.query.endDate;
}

app.js:

// This loads the environment variables from the .env file
require('dotenv-extended').load();

const builder = require('botbuilder');
const restify = require('restify');

// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
   console.log('%s listening to %s', server.name, server.url); 
});

// Create chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
    appId: process.env.MICROSOFT_APP_ID,
    appPassword: process.env.MICROSOFT_APP_PASSWORD
});

// Listen for messages from users 
server.post('/api/messages', connector.listen());

var bot = require("./bot")
bot.InitializeBot(connector);

// Load REST API module
var restApi = require("./rest_api");

并希望通过查询字符串获取参数(URL中的示例请求: bookinghandler?sender = 100000250242810& email=some@email.com& hotelName = Grand Hotel& confirmNumber = 654321& startDate = 2017-10-11& amp;; endDate = 2017-10-15 ).

And want to get parameters through query string (sample request in URL: bookinghandler?sender=100000250242810&email=some@email.com&hotelName=Grand Hotel&confirmNumber=654321&startDate=2017-10-11&endDate=2017-10-15).

但是 req.params req.param() req.body req.query 是仍然是空的.我做错了什么?这是通过 require('url') require('qs') ?如果我只想通过快递吗?

However req.params, req.param(), req.body, req.query are still empty. What I'm doing wrong? Is it the only way to do it through require('url') or require('qs')? If I want to it through express only?

推荐答案

问题出在使用2种REST框架:express和restify.需要使整个应用程序重新定义或表示应用程序.在我拥有一台Restify服务器之前,在其中创建了另一台Express服务器(只是采用了不同的示例).通常需要使用其中一个,而不是两者都使用.

Problem was in using 2 REST frameworks: express and restify. Need to make entire application as restify or express app. Before I had a restify server and inside that another express server was created (just took different examples). Generally need to use one or the other, not both.

修改后的app.js:

// This loads the environment variables from the .env file
require('dotenv-extended').load();

const builder = require('botbuilder');
const restify = require('restify');

// Setup Restify Server
var server = restify.createServer();
server.use(require('restify-plugins').queryParser());
server.listen(process.env.port || process.env.PORT || 3978, function () {
   console.log('%s listening to %s', server.name, server.url); 
});

// Index route
server.get('/', function (req, res) {
    res.send('My chat bot')
});

// Create chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
    appId: process.env.MICROSOFT_APP_ID,
    appPassword: process.env.MICROSOFT_APP_PASSWORD
});

// Listen for messages from users 
server.post('/api/messages', connector.listen());

var bot = require("./bot")
bot.InitializeBot(connector);

// Load REST API module
var restApi = require("./rest_api");

// POST request from webview page
server.post('/bookinghandler', restApi.BookingHandler);

修改后的rest_api.js:

const service = require("./service");

module.exports.BookingHandler = async function (req, res, next) {
    let senderId = req.query.sender;
    let email = req.query.email;
    let hotelName = req.query.hotelName;
    let confirmNumber = req.query.confirmNumber;
    let start = req.query.startDate;
    let end = req.query.endDate;

    res.send(200);  // Success
    return next(false);
};

这篇关于在Express 4中查询字符串变量仍然为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆