Reputation: 3
I am new to node.js, I have a requirement of parsing request body in the file named 'message.txt'. Which works fine when I logged the message variable on the console. The code is simple, first, it creates a server then receives a request on the post method then saves it to the file named 'message.txt'. I have tried to change the variable name but still, the error can't be solved.
const http = require('http');
const fs = require('fs');
const server = http.createServer((req,res)=>{
const url= req.url;
const method = req.method;
if(url=='/'){
res.write('<html>');
res.write('<head><title>enter message</title><head>');
res.write('<body><form action="/message" method="POST"><input type="text" name="message"><button type="submit">submit</button></form></body>');
res.write('</html>');
return res.end();
}
if(url=='/message'&&method=='POST'){
const body =[];
req.on('data',(chunk)=>{
body.push(chunk);
console.log(body)
});
req.on('end',()=>{
const parsedBody = Buffer.concat(body).toString();
// console.log(parsedBody);
const message = parsedBody.split('=')[1];
//console.log(messsage)
});
fs.writeFileSync('message.txt',message);
res.statusCode = 302;
res.setHeader('Location','/')
return res.end();
}
res.setHeader('Content-Type', 'text/html');
res.write('<html>');
res.write('<head><title>my first page!</title><head>');
res.write('<body><h1>hello from node js</h1></body>');
res.write('</html>');
res.end();
});
server.listen(3000)
Upvotes: 0
Views: 1037
Reputation: 787
You're using local defined message
variable out of the callback function. Even if you define this var globally there is poor likelihood the variable won't have NULL
value during passing it to fs.writeFileSync
.
So just specify your fs.writeFileSync('message.txt', message);
withing req.on('end', () => { ... }
after the line you define the message
var.
Upvotes: 0
Reputation: 1325
Constant variables are block scoped, read more about them here.
In your code, there is this piece:
req.on('end',()=>{
const parsedBody = Buffer.concat(body).toString();
// console.log(parsedBody);
const message = parsedBody.split('=')[1];
//console.log(messsage)
});
fs.writeFileSync('message.txt',message);
Here, you are passing an arrow function as a callback on the second argument of req.on
.
This arrow function has it's own block scope
.
By declaring the message
variable within that block scope, you won't be able to access it outside of it.
And, on the next line you where you call fs.writeFileSync
you are trying to do just that, accessing the message
variable which doesn't exist in that scope.
Thus, move that line in the scope:
req.on('end',() => {
const parsedBody = Buffer.concat(body).toString();
const message = parsedBody.split('=')[1];
fs.writeFileSync('message.txt', message); // here message can be accessed
});
Upvotes: 1
Reputation: 466
If
block should be like
if(url=='/message'&&method=='POST'){
const body =[];
req.on('data',(chunk)=>{
body.push(chunk);
console.log(body)
});
var message = '';
req.on('end',()=>{
const parsedBody = Buffer.concat(body).toString();
// console.log(parsedBody);
message = parsedBody.split('=')[1];
//console.log(messsage)
});
fs.writeFileSync('message.txt',message);
res.statusCode = 302;
res.setHeader('Location','/')
return res.end();
}
Basically your message
variable's scope was limited to req.on()
and thus not accessible outside.
Upvotes: 1