Reputation: 7782
This is what I got. It works great but I'd like to be able to send a file and data (JSON), to the client when he logs in my website. Is there any way to combine that?
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
Upvotes: 3
Views: 3501
Reputation: 7782
You can't send 2 files at once. But you can embed the JSON inside the html by using a template library with ejs
.
Upvotes: 1
Reputation: 424
A stream can send only one type of content for a request. However, depending on your Accept headers, you can send different content for different requests on the same request URL
app.get('/', function (req, res) {
if(req.accepts('text/html')){
res.sendfile(__dirname + '/index.html');
return;
}
else if(req.accepts('application/json')){
res.json({'key':'value'});
return;
}
});
Here if your request header accepts 'text/html'
, it will return the index.html file. and if the request header accepts 'application/json'
it will return the JSON response.
Upvotes: 0