Reputation: 1143
I'm trying to start a localhost server via nodejs and mySql database. I'm getting an error which says:
SyntaxError: Unexpected token ILLEGAL at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:413:25) at Object.Module._extensions..js (module.js:452:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Module.require (module.js:365:17) at require (module.js:384:17) at /Users/Liran/Desktop/nodejs/server.js:34:13 at Array.forEach (native) at Object. (/Users/Liran/Desktop/nodejs/server.js:32:7)
this error is caused by running the following javascript code:
var express = require('express');
var app = express();
var mysql = require('mysql')
var fs = require('fs'),
path = require('path');
var ipaddress = '127.0.0.1';
var port = 8080;
// ----------- MySQL Connection ------
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '1',
});
connection.connect();
// --------- Set upload directory -------
app.configure(function(){
app.use(express.methodOverride());
app.use(express.bodyParser({ keepExtensions: true, uploadDir: '../public/images/' }));
});
app.fs = fs;
// ------------ ROUTES ---------------
var RouteDir = 'routes',
files = fs.readdirSync(RouteDir);
files.forEach(function (file) {
var filePath = path.resolve('./', RouteDir, file),
route = require(filePath);
route.init(app, connection);
});
// ----------- Run Server ----------------
app.listen(port, ipaddress, function()
{
console.log('%s: Node server started on %s:%d ...',
Date(Date.now() ), ipaddress, port);
});`enter code here`
basically all I'm doing is to set the server ip & port and connect it to the mysql db.. please tell me what am I doing wrong and why is that error appears? and if there are any code changes that I should make in order to make a good localhost server via nodejs and mysql please let me know :) Thanks in advance!
Upvotes: 0
Views: 2729
Reputation: 433
In your code
// ----------- MySQL Connection ------
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '1',
});
connection.connect();
you have an extra coma next to the password.
Thats the only actual error I see
Upvotes: 0
Reputation: 106736
One of the files you are require()
ing in your files.forEach()
callback has an illegal token. You may want to insert a debug statement before the require()
so you can determine which file is having the problem. From there, you can use a linter like jshint
to find the exact location of the illegal token in that file.
Upvotes: 2