Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query string variables still empty in Express 4

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");

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

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?

like image 778
Aleksey Kontsevich Avatar asked Dec 03 '25 05:12

Aleksey Kontsevich


1 Answers

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.

Modified 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);

Modified 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);
};
like image 79
Aleksey Kontsevich Avatar answered Dec 05 '25 19:12

Aleksey Kontsevich