"语法错误:意外的令牌N'QUOT;在Ex preSS节点服务器 [英] "SyntaxError: Unexpected token n" in Express Node Server

查看:364
本文介绍了"语法错误:意外的令牌N'QUOT;在Ex preSS节点服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个错误 -

I get this error-

SyntaxError: Unexpected token n
    at parse (/Users/afroza/node_modules/body-parser/lib/types/json.js:83:15)
    at /Users/afroza/node_modules/body-parser/lib/read.js:116:18
    at invokeCallback (/Users/afroza/node_modules/raw-body/index.js:262:16)
    at done (/Users/afroza/node_modules/raw-body/index.js:251:7)
    at IncomingMessage.onEnd (/Users/afroza/node_modules/raw-body/index.js:308:7)
    at IncomingMessage.emit (events.js:104:17)
    at _stream_readable.js:908:16
    at process._tickCallback (node.js:355:11)

我创建具有一个的NodeJS路线和我得到这个错误,我尝试过很多事情,没有办法,总是出现这个错误,

I creating a route with nodejs and I'm getting this error,I've tried many things and there's no way, always appears this error,

我的服务器设置在离preSS框架 -

my server setup in express framework-

var express = require('express');

var app = express()
    , fs = require('fs')
    , path = require('path');

var env = process.env.NODE_ENV || 'production'
    , config = require('./config/config')[env]
    , mongoose = require('mongoose');

var http = require('http');

var db = mongoose.connect('mongodb://localhost/orbitax');


app.use(express.static(__dirname + '/public'));


var bodyParser = require('body-parser'); // add body-parser for interact with server and client data


var jsonParser = bodyParser.json(); //join this body parser with json file

var urlencodedParser = bodyParser.urlencoded({ extended: false});


//Initialize Models
var models_path = __dirname + '/app/models'
fs.readdirSync(models_path).forEach(function (file) {

  require(models_path+'/'+file)
});

require('./config/routes')(app,config);

var server = app.listen(9000, function(){
    var host = server.address().address;
    var port = server.address().port;

});

服务器的路由,

var async = require('async')
    , mongoose = require('mongoose')
    , uploadModel = mongoose.model('uploadModel');


module.exports = function (app) {


  // upload routes

  var upload = require('../app/controller/upload');

  app.get('/newUpload', upload.newUpload);

  app.post('/create_new_directory', upload.create_new_directory);


}



exports.create_new_directory = function(req, res)
{
    console.log("hello")
    console.log(req.body);
}

和来自客户端的请求,

$http({'method': 'post', 'url': '/create_new_directory', data: $scope.directory })
            .success(function(data){
                console.log(data)
            })
            .error(function(){

            })

任何帮助将是AP preciated,谢谢你。

Any help would be appreciated, Thanks.

推荐答案

默认情况下, $ HTTP 服务只请求数据转换成一个JSON字符串如果数据一个东西。

By default, the $http service will only transform request data to a JSON string if the data is an object.

https://开头github上。 COM /角/ angular.js / BLOB / V1.4.8 / src目录/ NG / http.js#L292

在你的情况下,请求数据是一个字符串,所以它是没有得到包裹在引号,使它无效JSON。

In your case, the request data is a string so it is not getting wrapped in quotes, making it invalid JSON.

您应该能够修改 $ HTTP 配置是这样的:

You should be able to modify your $http config like this:

$http({
    method: 'POST',
    url: '/create_new_directory',
    data: $scope.directory,
    transformRequest: JSON.stringify
}).success(function(data){
    console.log(data)
})

这篇关于"语法错误:意外的令牌N'QUOT;在Ex preSS节点服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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