node.js 语法错误:意外的标识符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28443537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:25:02  来源:igfitidea点击:

SyntaxError: Unexpected identifier

node.jsexpress

提问by Nafa ? Lamine

I am new to node.js. I created a file named app.jsand put this code in that file using express to switch the template engine:

我是 node.js 的新手。我创建了一个名为的文件app.js并将此代码放入该文件中,使用 express 切换模板引擎:

//module dependencies

var express = require('express');
    routes = require ('./routes');
    user = require ('./routes/user');
    http= require ('http');
    path = require ('path');

var exphbs = require ('express3-handlebars');
var app = express();

//all environement
app.set ('port', process.env.PORT || 3000);
app.set('views', __dirname +'/views');
//app.set('view engine','jade');
app.engine('handlebars',exphbs({defaultLayout :'main'}));
app.set('view engine ','handlebars');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname,'public')));

//developpement only
if ('developpement' == app.get('env')){
    app.use(express.errorHandler());
}

//app.get('/', routes.index);
//app.get ('/user' , user.list);
app.get('/' , function(req,res) {
    res.render('home');
}
http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

Then I run the application and get this error:

然后我运行应用程序并收到此错误:

http.createServer(app).listen(app.get('port'), function(){
^^^^
SyntaxError: Unexpected identifier
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:929:3

This line is causing the error:

此行导致错误:

http.createServer(app).listen(app.get('port'), function(){

回答by Robert Rossmann

You are missing a closing brace in

你缺少一个右括号

app.get('/' , function(req,res) {
  res.render('home');
}) // <-- the last one

You should use an editor that provides proper syntax highlighting and a code linter - like jshintwhich would warn you about this and also warn you about improper variable declarations:

您应该使用提供正确语法突出显示和代码linter的编辑器 - 比如jshint,它会警告您这一点,也会警告您不正确的变量声明:

var onevar = 'value'; // <-- superbad! You just ended this statement!
    another = 'val2'; // <-- now this variable leaked into global scope!
// Proper:
var onevar = 'value';
var another = 'val2';
// Also proper:
var onevar = 'value',
    another = 'val2';

The SyntaxError: Unexpected identifieris always a typo (or you trying to do something JavaScript does not understand) somewhere in your code and usually happens beforethe unexpected identifier. Oversimplified, it basically means that the parser was in the middle of some statement and, according to the grammar rules, the thing that followed was not acceptable for that particular situation.

SyntaxError: Unexpected identifier始终是代码中某个地方的拼写错误(或者您试图做一些 JavaScript 无法理解的事情),并且通常发生意外的标识符之前。简单来说,它基本上意味着解析器处于某个语句的中间,并且根据语法规则,后面的内容对于该特定情况是不可接受的。

回答by siavolt

I think this incorrect:

我认为这是不正确的:

var express = require('express');
    routes = require ('./routes');
    user = require ('./routes/user');
    http= require ('http');
    path = require ('path');

I think you must try something like that:

我认为你必须尝试这样的事情:

var express = require('express'),
    routes = require ('./routes'),
    user = require ('./routes/user'),
    http= require ('http'),
    path = require ('path');

Or

或者

var express = require('express');
var routes = require ('./routes');
var user = require ('./routes/user');
var http= require ('http');
var path = require ('path');