+ 3
CSS doesn't load into my HTML code using NODE.js
When i read files from node js its not reading css files. and also I want to mention that my css files are connected to html, when I run it on it own its showing it correctly, but when I run it from node server its just only showing html files without styles
3 Answers
+ 3
const http = require('http');
const fs = require('fs');
const url = require('url');
const server = http.createServer(function(req,res){
fs.readFile( "index.html", + "css/index.css", function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end();
});
}).listen(8089);
console.log('Server is lisening on port 8089');
+ 3
*My directory set up like thisđ
css[folder]
index.css
js [folder]
index.js
index.html
server.js
+ 3
Use express
app.js:
const express = require("express");
const app = express();
app.use(express.static("public"));
app.get('/', (req, res) => {
res.sendFile('index.html');
})
app.listen(3000, function() {
console.log("Running on port 3000.");
});
Then, put your resources (html and css) in a folder called public.
Here is a git repo demo for you:
https://github.com/gorgorgordon/node_css_demo
After cloning, run
npm install express
first to install express
before using node app.js