“”使用Express for Node.js
以下工作对我来说非常好:
app.get('/doi/meta/:doiName1/:doiName2', function(request, response) { var path = '/doi/json?doi='+request.params.doiName1+'/'+request.params.doiName2; // etc.
例如,我可以打电话给:
curl -X GET http://localhost:1337/doi/meta/09.1010/9347426
并得到我期待的答复。 (请注意我查询的对象的名称有一个斜杠。)
由于在对象名称中存在一些潜在的可变性,因此我需要更改服务器,以便可以像这样构build查询:
curl -X GET http://localhost:1337/doi/meta?doiName=09.1010/9347426
我尝试了很多方法,但我总是得到以下回应:
[SyntaxError: Unexpected end of input]
这表明我在错误的地方寻找问题。 但是,我可以将服务器代码更改回上述,它工作正常。 我的代码目前看起来像这样:
app.get('/doi/meta', function(request, response) { //var path = '/doi/json?doi='+request.params.doiName1+'/'+request.params.doiName2; var args = url.parse(request.url, true).query; var path = 'doi/json?doi='+args['doiName']; console.log('path is '+path); // etc.
console.log语句永远不会到达。 什么是导致此意外input错误结束的问题?
SyntaxError: Unexpected end of input
错误的SyntaxError: Unexpected end of input
通常意味着您的代码中某处丢失了右括号,大括号,括号或类似的字符,或者您尝试使用JSON.parse()
缺less此类字符的string。
通过javascript linter (例如jshint )运行你的代码/ JSONstring来查找语法错误。
发生了什么事情是在同一个文件中有几百行以上有另一个GET侦听器:
app.get('/doi/:user', function(request, response) {
这个街区正在迎头赶上:
http://localhost:1337/doi/meta?doiName=09.2815/4539580
并去寻找一个名为meta的用户。 当然,当我像这样构buildGET块时,这种现象不会发生:
app.get('/doi/meta/:doiName1/:doiName2', function(request, response) {
我将通过“深化”上面的第一个听众来解决这个问题,例如…
app.get('/doi/user/:user', function(request, response) {
另一个教训是在相同的文件中将类似的URL调用分组在一起。
感谢您的回应。