Reputation: 6790
I am trying to do my first ever node.js web server (local) however I can't seem to get it started, below is the code and the error message.
var app = require('express');
app.configure(function(){
app.set('port', 8080);
app.use(express.logger('dev')); /* 'default', 'short', 'tiny', 'dev' */
app.use(express.bodyParser());
app.use(express.static(path.join(__dirname, '/public')));
}
app.listen(8080);
Error message
app.listen(8080);
^^^
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
Upvotes: 1
Views: 566
Reputation: 4197
you should follow the basic tutorial from ebook or internet.
e.g., https://www.tutorialspoint.com/nodejs/nodejs_express_framework.htm
the simple node express app looks very clean and easy to learn
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World');
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
Upvotes: 0
Reputation: 479
I do not know if you've already found an answer to your question... But when I look at the code, I see some missing brackets when requiring the express module.
var app = require('express');
Here is the example "Hello World" snippet from the express website
var express = require('express')
var app = express()
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
They have two lines of code that first includes the express module into the project as the variable "express". Then, they initialize a new express instance (not really, but almost the same):
var app = express();
and THEN they call all functions related to "app". The two first lines of code in the above example is the same as this one line code:
var app = require('express')();
And as you can see, you are missing two brackets.
I also want to point out that you are missing a closing bracket and semi-colon at the very end of your configure function. It should look like this:
app.configure(function(){
app.set('port', 8080);
app.use(express.logger('dev')); /* 'default', 'short', 'tiny', 'dev' */
app.use(express.bodyParser());
app.use(express.static(path.join(__dirname, '/public')));
});
So... here is the final code:
var app = require('express')();
app.configure(function(){
app.set('port', 8080);
app.use(express.logger('dev')); /* 'default', 'short', 'tiny', 'dev' */
app.use(express.bodyParser());
app.use(express.static(path.join(__dirname, '/public')));
});
app.listen(8080);
Unfortunately the logger and bodyParser middlewares is not longer bundled with express, so you have to install them separately. app.configure is also outdated, so you can get the code to work if you remove the configure function completely, and install the middlewares.
NOTE You are also using path which you have not included to your project install it and add this in the top:
var path = require('path');
So, without any middleware installation, this is the final code that works with the latest version of node and express:
var express = require('express');
var path = require('path');
var app = express();
app.set('port', 8080);
app.use(express.static(path.join(__dirname, '/public')));
app.listen(8080);
OR
var app = require('express')();
var path = require('path');
app.set('port', 8080);
app.use(require('express').static(path.join(__dirname, '/public')));
app.listen(8080);
Upvotes: 1
Reputation: 23214
In express 3.x
You miss )
at end of configure method
app.configure(function(){
app.set('port', 8080);
app.use(express.logger('dev')); /* 'default', 'short', 'tiny', 'dev' */
app.use(express.bodyParser());
app.use(express.static(path.join(__dirname, '/public')));
});
Upvotes: 0
Reputation: 70055
You have many errors in your code. For example, the opening parenthesis on line 3 is never closed.
And it looks like you are trying to use some things that are currently deprecated in Express.
Here is your code modified to work with Express 3.20.2. You will get a pair of deprecation warnings but the code will work.
var path = require('path');
var express = require('express');
var app = express();
app.set('port', 8080);
app.use(express.logger('dev')); /* 'default', 'short', 'tiny', 'dev' */
app.use(express.bodyParser());
app.use(express.static(path.join(__dirname, '/public')));
app.listen(8080);
The above code will not run as-is in Express 4. But hopefully this gets you started down a more productive path. If you are following along with a tutorial, find one that covers a more recent version of Express.
Upvotes: 1