Is it necessary to set a Content-Type in Node.js?

后端 未结 4 632
难免孤独
难免孤独 2021-01-21 08:33

Just started playing with Node.js and after seeing a few examples I see that usually the Content-Type is set before returning some content.

Usually somethin

4条回答
  •  遇见更好的自我
    2021-01-21 09:08

    Of course not, if you are playing with NodeJs. But to make a maintainable code with different modules, large and API pages you should have 'Content-Type' in your server definition.

    const http = require('http');
    
    const host_name = 'localhost';
    const port_number = '3000';
    
    const server = http.createServer((erq, res) => {
        res.statusCode = 200;
        // res.setHeader('Content-Type', 'text/html');
        res.end("

    Head Line


    Line 2
    Line 3"); }); server.listen(port_number, host_name, ()=> { console.log(`Listening to http://${host_name}:${port_number}`); });

提交回复
热议问题