简体   繁体   中英

throw err in node (in CSS)

when i try to run nodejs, there is an error in "throw er;" in the "css" part, but it doesn't exist. Someone can see the error?

var http = require('http'),
fs = require('fs');

    http.createServer(function(req, res) {

            fs.readFile('./index.html', function (err, data) {
                if (err) { throw err; }
                    res.writeHeader(200, {'Content-Type': 'text/html'});
                    res.write(data);
                    res.end();

            });


            fs.readFile('./main.js', function (err, data) {
                if (err) { throw err; }
                    res.writeHead(200, {'Content-Type': 'text/javascript'});
                    res.end(data);
                    res.end();
            });

            fs.readFile('./style.css', function (err, data) {
                if (err) { throw err; }
                    res.writeHead(200, {'Content-Type': 'text/css'});
                    res.write(data);
                    res.end();
            });

    }).listen(8000);

you have a few problems in your code:

  1. at the main.js part you use res.end instead of res.write
  2. you call res.end 3 times
  3. you try to return 3 files in one request, you should check the path ( req.url ) and return the right file by that. or you can use express or other framework.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM