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:
main.js
part you use res.end instead of res.write 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.