Node.js post方法失败,出现500错误

时间:2016-04-21 14:04:34

标签: node.js azure azure-web-sites

我是node.js的新手。我正在使用VS 2015开发节点js应用程序。下面是我的服务器代码(app.js)

在其中一条路线(train.js)中,我定义了一个简单的post方法,如下所示。事情是从localhost工作正常。但是一旦我部署到azure网站,POST方法就会抛出内部服务器错误。 有人可以帮我弄这个吗?如果您需要任何进一步的细节,请告诉我。

//Here is my app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var socketio   = require('socket.io');


var global = require('./routes/globals.js');

GLOBAL._ = require('lodash');
GLOBAL.KEY = 'xxxxxxxxxxxxxxxxxxxxxx';

var availability = require('./routes/availability');
var fare = require('./routes/fare');
var stations = require('./routes/stations');
var pnr = require('./routes/pnr');
var route = require('./routes/route');
var trains = require('./routes/trains');

var app = express();
app.socket = socketio();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));

app.set('port', process.env.PORT || 3000);
app.use('/availability', availability);
app.use('/fare', fare);
app.use('/stations', stations);
app.use('/pnr', pnr);
app.use('/route', route);
app.use('/trains', trains);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function (err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});

app.socket.on('connection', function (socket) {
    socket.on('message', function (msg) {
        console.log('Message Received: ', msg);
        socket.broadcast.emit('message', msg);
    });
});
global.io = app.socket;
module.exports = app;

//Here is my train.js
var express = require('express');
var http = require('http');
var router = express.Router();
var utils = require("./utils.js");
var global = require('./globals.js');

router.get('/', function (req, res) {
    var options = {
        host: 'www.xxxxxxx.com',
        path: '/test/xxxxxx/'
    };

    var parameters = {
        fscode : 'xxxx',
        tscode: 'xxxx',
        //date: '',
        //'class': '',
        //orderby: '',
        format: 'json',
        pbapikey: '9xxxxxxxxa'
    };

    options.path += utils.getQueryString(parameters);
    options.path += "pbapisign/" + utils.getHmacSHA1Signature(parameters);

    callback = function (response) {
        var data = "";
        response.on('data', function (chunk) {
            data += chunk;
        });
        response.on('end', function () {
            console.log(data);
            res.send(data + "<h1>" + utils.hello() + "</h1>");
        });
    }

    http.request(options, callback).on('error', function (error) {
        console.log(error);
        res.status(400).send(error.Message)
    }).end();

});

router.post('/check', function (req, res) {
   // console.log('Request -', req);
   // console.log('Response -', res);
    var data = {
        request : "HELLO",
        response: "VAMSI"
    }
    global.io.sockets.emit('NEW_CONTACT', data);
    res.status(200).send('NEW DATA').end();

});



module.exports = router;

1 个答案:

答案 0 :(得分:1)

您的代码似乎没有任何明显的错误。

因此,如果您可以分享一些信息以获取更多详细信息,例如错误或部署,我认为这对调查问题很有帮助。

与此同时,请尝试为您的VS2015安装NTVS,然后按照Wiki页面Advanced Debugging - Remote Debugging on Windows Azure调试您的应用程序。