Making a chat application referencing via Node in action, and upon running the server.js, got the following error: function serveStatic(response,cache,absPath) ^^^^^^^^ SyntaxError: Unexpected token function at Object.exports.runInThisContext (vm.js:73:16) at Module._pile (module.js:543:28) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:488:32) at tryModuleLoad (module.js:447:12) at Function.Module._load (module.js:439:3) at Module.runMain (module.js:605:10) at run (bootstrap_node.js:418:7) at startup (bootstrap_node.js:139:9) at bootstrap_node.js:533:3
Here is the Server.js code:
var http=require('http');
var fs=require('fs');
var path=require('path');
var path= require('mime');
var cache={};
var server=http.createServer(function(request,response)
{
var filePath=false;
if(request.url=='/')
{
filePath= public/index.html;
}
else
{
'public' + request.url;
}
var absPath= './'+filePath;
serveStatic(response,cache,absPath);
});
server.listen(3000,function()
{
console.log('Server listening to the port :3000');
});
function send404 (response )
{
response.writeHead(404,{'Content-Type' :'text/plain'});
response.write('Error 404: resource not found');
response.end();
}
function sendFile(response,filePath,fileContents)
{
response.wrieHead(200,
{"content-type":mime.lookup(filePath)})
};
response.end(fileContents);
}
function serveStatic(response,cache,absPath)
{
if(cache[absPath])
{
sendFile(response,absPath,cache[absPath]);
}
else
{
if(fs.exists(absPath, function(exists)))
{
if(exists)
{
fs.readFile(absPath,function(err,data))
{
if(err)
{
send404(response);
}
else
{
cache[absPath]=data;
sendFile(response,absPath,data);
}
});
}
else
{
send404(response);
}
});
}
}
Making a chat application referencing via Node in action, and upon running the server.js, got the following error: function serveStatic(response,cache,absPath) ^^^^^^^^ SyntaxError: Unexpected token function at Object.exports.runInThisContext (vm.js:73:16) at Module._pile (module.js:543:28) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:488:32) at tryModuleLoad (module.js:447:12) at Function.Module._load (module.js:439:3) at Module.runMain (module.js:605:10) at run (bootstrap_node.js:418:7) at startup (bootstrap_node.js:139:9) at bootstrap_node.js:533:3
Here is the Server.js code:
var http=require('http');
var fs=require('fs');
var path=require('path');
var path= require('mime');
var cache={};
var server=http.createServer(function(request,response)
{
var filePath=false;
if(request.url=='/')
{
filePath= public/index.html;
}
else
{
'public' + request.url;
}
var absPath= './'+filePath;
serveStatic(response,cache,absPath);
});
server.listen(3000,function()
{
console.log('Server listening to the port :3000');
});
function send404 (response )
{
response.writeHead(404,{'Content-Type' :'text/plain'});
response.write('Error 404: resource not found');
response.end();
}
function sendFile(response,filePath,fileContents)
{
response.wrieHead(200,
{"content-type":mime.lookup(filePath)})
};
response.end(fileContents);
}
function serveStatic(response,cache,absPath)
{
if(cache[absPath])
{
sendFile(response,absPath,cache[absPath]);
}
else
{
if(fs.exists(absPath, function(exists)))
{
if(exists)
{
fs.readFile(absPath,function(err,data))
{
if(err)
{
send404(response);
}
else
{
cache[absPath]=data;
sendFile(response,absPath,data);
}
});
}
else
{
send404(response);
}
});
}
}
Share
Improve this question
asked Jul 3, 2017 at 12:20
raman bhayanaraman bhayana
991 gold badge1 silver badge8 bronze badges
4
-
2
filePath= public/index.html;
is missing quotes and'public' + request.url;
is a no-op that does nothing ... – Alex K. Commented Jul 3, 2017 at 12:21 - @AlexK. , got these missing quotes at the place now, still the same error as shown above – raman bhayana Commented Jul 3, 2017 at 12:25
-
you seem to have a few extra random
}
and a random)
as well - indent your code properly to see the problem – Jaromanda X Commented Jul 3, 2017 at 12:29 - @JaromandaX - Yeah having little extra braces , correct it out now and working fine. – raman bhayana Commented Jul 3, 2017 at 12:37
2 Answers
Reset to default 1You have extra brackets here:
function sendFile(response,filePath,fileContents)
{
response.wrieHead(200,
{"content-type":mime.lookup(filePath)})
};
response.end(fileContents);
}
Should be modified this way:
function sendFile(response, filePath, fileContents)
{
response.wrieHead(200, {
"content-type": mime.lookup(filePath)
});
response.end(fileContents);
}
Then your error will be dismissed.
In my case setting the buildOptimizer
from false
to true
worked.
From:
"development": {
"buildOptimizer": false,
"optimization": {
"scripts": true,
"styles": {
"minify": true,
"inlineCritical" : false
},
"fonts": true
},
"vendorChunk": true,
"extractLicenses": false,
"sourceMap": true,
"namedChunks": true
}
To:
"development": {
"buildOptimizer": true,
"optimization": {
"scripts": true,
"styles": {
"minify": true,
"inlineCritical" : false
},
"fonts": true
},
"vendorChunk": true,
"extractLicenses": false,
"sourceMap": true,
"namedChunks": true
}