简体   繁体   中英

Error in setting up server through express.js

This is my server.js file and I get the error mentioned below when I am trying to set up the server using Express.js

var express = require('express');
var app = express();

 var PORT = process.env.PORT || 3000;

 app.all('/*', function(req, res) {
    res.send('<!DOCTYPE html> <head> <title> Todo App </title> </head> <body> <h1>This is a Todo App </h1> </body> </html>');
});

app.listen(PORT, function() {
    //callback
    console.log('Server running on ' + PORT);
});

This is the error i am getting

(function (exports, require, module, __filename, __dirname) { ??v
                                                          ^
SyntaxError: Invalid or unexpected token
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:528:28)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.runMain (module.js:590:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

I have no problem running this bunch of code after improving the formatting and removed those "`" in the code.

var express = require('express');
var app = express();

 var PORT = process.env.PORT || 3000;

 app.all('/*', function(req, res) {
    res.send('\
        <!DOCTYPE html>\
            <head>\
                <title> Todo App </title>\
            </head>\
            <body>\
                 <h1>This is a Todo App </h1>\
            </body>\
       </html>\
    ');
});

app.listen(PORT, function() {
    //callback
    console.log('Server running on ' + PORT);
});

NOTE : you declared PORT variable but you used port variable instead in app.listen() .

Your issue is most likely based around the formatting of your multiline HTML string. Try sending something simpler like 'hello' to make sure it works. Then work your way forward by adding a few tags at a time until you find where the error is.

app.get('/', function(req, res) {
  res.send('hello');
});

Finally, you should really consider using a templating system like Pug or use res.sendFile() to return separate html files rather than trying to build raw HTML strings. The escaping you need to do on the raw strings will be incredibly difficult to debug since they're just strings so the javascript engine doesn't know what line they're faulting on.

app.get('/', function(req, res) {
  // __dirname will resolve to your project folder
  res.sendFile(path.join(__dirname + '/index.html'));
});

I had the same problem. It was caused by file coding. I mean I create server.js file by powershell command '' >> server.js then I saw that error but when I created file by IDE with the same content error disappear.

I think to answer your question we need more information about your environment and what versions of things you are using:

 node -v # v6.11.1
 npm -v # 3.10.10

Here are the literal steps I did on Mac OS and bash and was successful

$ mkdir test-express
$ cd test-express
$ npm install express
$ cat <<EOF > index.js
var express = require('express');
var app = express();

var PORT = process.env.PORT || 3000;

app.all('/*', function(req, res) {
    res.send('<!DOCTYPE html> <head> <title> Todo App </title> </head> <body> <h1>This is a Todo App </h1> </body> </html>');
});

app.listen(PORT, function() {
    //callback
    console.log('Server running on ' + PORT);
});
EOF
$ node index.js
Server running on 3000

And the curl:

$ curl localhost:3000
<!DOCTYPE html> <head> <title> Todo App </title> </head> <body> <h1>This is a Todo App </h1> </body> </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM