If I have some js or css files in the views folder,then how can I write the path to link thoes files?this is my file structure
Asked
Active
Viewed 63 times
-1
-
You need to do some reading. To give you pointers: `__dirname` is a global variable provided by node.js which gives you the directory path that the currently executing script resides in. Refer this [documentation link](https://nodejs.org/api/globals.html#globals_dirname) . Also, refer [this](http://stackoverflow.com/questions/8131344/what-is-the-difference-between-dirname-and-in-node-js) question on why you should use `__dirname` instead of `./` – A.I Dec 04 '15 at 09:53
1 Answers
1
you can add the script path into your view file to call them.
var http = require('http'),
fs = require('fs');
http.createServer(function (req, res) {
if(req.url.indexOf('.js') != -1){ //req.url has the pathname, check if it conatins '.js'
fs.readFile(__dirname + '/script.js', function (err, data) {
if (err) console.log(err);
res.writeHead(200, {'Content-Type': 'text/javascript'});
res.write(data);
res.end();
});
}
if(req.url.indexOf('.css') != -1){ //req.url has the pathname, check if it conatins '.css'
fs.readFile(__dirname + '/style.css', function (err, data) {
if (err) console.log(err);
res.writeHead(200, {'Content-Type': 'text/css'});
res.write(data);
res.end();
});
}
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
Thats just to give you and idea !

Profstyle
- 434
- 4
- 20