Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
76
rated 0 times [  78] [ 2]  / answers: 1 / hits: 17191  / 8 Years ago, sat, december 31, 2016, 12:00:00

I have code that reads the file 'example.js' and sends it to the client.



app.get('/mods/example.js', function(req, res) {
fs.readFile('./mods/example.js',
{encoding: 'utf-8'},
(err, data) => {
if (!err)
{
res.send(var example = new Mod(); + data);
}
});
});


The problem is how do I send the response as a javascript file?



When I open the file in the web browser it is an html file not a javascript file.



Thanks in advance!


More From » node.js

 Answers
6

As suggested by noisypixy, I used the res.type function to change the type of the response to javascript.



res.type('.js');
res.send(var john = new Human(););


There are a lot of other file types such as html, json, png.



API reference with example code: http://expressjs.com/en/4x/api.html#res.type


[#59505] Wednesday, December 28, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kevonmoisesf

Total Points: 693
Total Questions: 101
Total Answers: 128

Location: Reunion
Member since Mon, Dec 28, 2020
3 Years ago
kevonmoisesf questions
Sat, Jan 23, 21, 00:00, 3 Years ago
Tue, Feb 18, 20, 00:00, 4 Years ago
Wed, Jun 12, 19, 00:00, 5 Years ago
;