How can I write to a server file using Node FS?

Asked

Viewed 56 times

-1

The command is running on my server machine:

$ http-server . -p 3333

Everything perfect when accessing from a web browser to another machine (also Linux).

But when running this script on Node, I cannot write to a file that is on the server, nor does it return an error. Why Fs writeFile does not work with an IP address? Is there any solution for this?

app.post('/formulario', (req, res) => {
	nome = req.body.nome
	idade = req.body.idade
	console.log('Nome.:', nome, '\nIdade:', Number(idade)+10)
	res.send('Nome.: '  + nome + '<br>Idade: ' + idade)
	fs.writeFile('http://192.168.0.113:3333/arquivoTeste.txt', 'Nome.: '+ nome, function (err) {
		if (err) throw err;
		console.log('Escrito!');
	})
	res.end()
})

1 answer

0

Good morning!

For you to access something you must pass the path from where it is, in your case you try to access an ip and a port but this does not lead you to any directory inside the machine, for example, an ftp gives you direct access to a folder inside the machine via ip + port (example: 192.168.0.5:21), another thing because you do not use a database to be able to store your information instead of a file?

  • Thanks Gustavo. I was able to access the app from another machine through the port, etx and I realized that saved and accessed the data perfectly. It met the need. It was something simple so I did not opt for a database.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.