I am new to node.js. I created a file named app.js
and put this code in that file using express to switch the template engine:
//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(){
Syntax Error – This error is caused by an error in the PHP structure when a character is missing or added that shouldn't be there. Unexpected – This means the code is missing a character and PHP reaches the end of the file without finding what it's looking for.
As you write your JavaScript application, the unexpected token error always occurs because JavaScript expected a specific syntax that's not fulfilled by your current code. You can generally fix the error by removing or adding a specific JavaScript language symbol to your code.
The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.
Unexpected TokenThis is simply down to a syntax error (your fault, I'm afraid). Perhaps you forgot the ':' after a conditional branch or there is an unclosed parenthesis left somewhere in your code? Python scripts are broken down into 'tokens' which helps the program navigate the logic of your code.
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 jshint which would warn you about this and also warn you about improper variable declarations:
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 identifier
is always a typo (or you trying to do something JavaScript does not understand) somewhere in your code and usually happens before the 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With