Answer the question
In order to leave comments, you need to log in
Throws Uncaught SyntaxError: Unexpected identifier?
Displays the error client.js:1 Uncaught SyntaxError: Unexpected identifier in the browser console.
It doesn't matter if there is code inside or not.
I climbed a little and found out that the whole thing is in the server.
Scripts:
var http = require('http'),
fs = require('fs');
http.createServer(function(req, res) {
if(req.url == "/") {
fs.readFile("index.html", function(err, content) {
if(err) {
console.log("error");
return;
}
res.end(content);
})
} else {
res.end("Bad request");
}
}).listen(3000);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Node.JS</title>
</head>
<body align="center">
<h1>Hello man!</h1>
<textarea id="theText" placeholder="Input the Text" cols="45" rows="10"></textarea>
<br>
<input type="button" id="sendText" value="Send Me">
<script type="text/javascript" src="client.js"></script>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
Try like this:
var http = require('http'),
fs = require('fs');
http.createServer(function(req, res) {
if(req.url == "/") {
fs.readFile("index.html", {encoding: 'utf8'}, function(err, content) {
if(err) throw err;
res.end(content);
});
} else if(req.url.includes('client')) {
fs.readFile("client.js", {encoding: 'utf8'}, function(err, content) {
if(err) throw err;
res.end(content);
});
} else {
res.end("Bad request");
}
}).listen(3000);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question