I'm starting to study Node.js. I purchased the manual written by Marc Wandscheider. I downloaded the tools to use it and I also downloaded Brackets.
I'm trying a sample script, but I get two errors that do not understand and that are not present in the guide.
The first error tells me that:
'require' was used before it was defined
C:\node> node debug web.js
<Debugger listening on port 5858>
connecting ... ok
break in C:\node\web.js: 1
1 var http = require ("http");
2
3 process_request function (req, res) {
debug>
while the second (in Brackets):
missing use strict statement
I saw on the internet that I can add the line
"use strict";
But the guide does not use it - is it required?
How can I fix these issues?
entire code
var http = require("http");
function process_request(req, res) {
var body = 'Thanks for calling!';
var content_length = body.length;
res.writeHead(200, {
'Content-Length': content_length,
'Content-Type': 'text/plain'
});
res.end(body);
}
var s = http.createServer(process_request);
s.listen(8080);
Those errors are actually suggestions of the JSHINT process to validate good code. Brackets is using it behind the scene probably. If you tell jshint you are writing for node then require becomes a global variable so it does not give that error. Try running this code with some warnings for JSHINT some article with proper explanation on using JSHINT
/*jshint node:true */
'use strict';
var http = require('http');
function process_request(req, res) {
var body = 'Thanks for calling!';
var content_length = body.length;
res.writeHead(200, {
'Content-Length': content_length,
'Content-Type': 'text/plain'
});
res.end(body);
}
var s = http.createServer(process_request);
s.listen(8080);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With