表情符号导致Express服务器崩溃

时间:2015-04-23 03:27:10

标签: javascript node.js express emoji

我正在尝试使用表情符号创建一个POST到我的Express端点:

curl --data '{"x": 10, "y":10, "z":10, "message": "", "userToken": "Marine"}' --header "Content-Type:application/json" localhost:3000/api/messages

出于某种原因,这会导致我的服务器崩溃:

SyntaxError: Unexpected token
    at Object.parse (native)
    at parse (/Users/user/Documents/uncovery/node_modules/body-parser/lib/types/json.js:84:17)
    at /Users/user/Documents/uncovery/node_modules/body-parser/lib/read.js:102:18
    at IncomingMessage.onEnd (/Users/user/Documents/uncovery/node_modules/body-parser/node_modules/raw-body/index.js:149:7)
    at IncomingMessage.g (events.js:199:16)
    at IncomingMessage.emit (events.js:104:17)
    at _stream_readable.js:908:16
    at process._tickCallback (node.js:355:11)

我去了我的路由器并将console.log放在我的/messages端点中以查看收到的内容但是当Express因发送表情符号而崩溃时,console.log永远不会激活。

router.js

var models = require('../db/models.js');
var util = require('../core/utilities.js');
var sockets = require('../routes/sockets.js');

module.exports = function(router) {
  //input: {x: float, y: float, z: float, message: string, userToken: string}
  router.post('/messages', function(req, res) {
    console.log("Emoji: ", req.body);
    models.createMessage(req.body).then(
      util.resolvePOST.bind(this, req, res),
      util.rejectPOST.bind(this, req, res)
      );
  });
}

我怀疑这与中间件有关并且实际上与路线无关,但是当我谷歌emojis crash node js时,我无法找到任何相关信息。为什么会这样?

index.js

var http = require('http');
var morgan = require('morgan');
var express = require('express');
var socketIO = require('socket.io');
var bodyParser = require('body-parser');

var util = require('./server/core/utilities.js');
var router = require('./server/routes/router.js');
var sockets = require('./server/routes/sockets.js');

var app = express();
var server = http.Server(app);

app.use(morgan('combined', util.getLogStream()));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json({ limit: '50mb' }));
app.use('/api', router);
app.use(express.static(__dirname + '/server/landing'));

var io = socketIO(server);
sockets.initialize(io);

server.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;
  console.log('Uncovery listening at http://%s:%s', host, port);
});

0 个答案:

没有答案