xbee
xbee

Reputation: 161

node js. how do i call local file from html

i have this file to run app.js

var express = require('express')
var app = express();
var router = express.Router();
var path = __dirname + '/views/';


router.get("/",function(req,res){
 res.sendFile(path + "index.html");
});

app.use("/",router);

app.use("*",function(req,res){
 res.sendFile(path + "index.html");
});

app.listen(8080,function(){
 console.log("Live at Port 8080");
});

and i have "index.html" inside of "views" folder. i want to import my local css file inside of "css" folder. i tried to call the file like this

<link href="css/main.css">

or

<link href="../css/main.css">

but it didnt work. is there any specific method to get them?

thanks

Upvotes: 2

Views: 1053

Answers (3)

Chandrakant Thakkar
Chandrakant Thakkar

Reputation: 978

Put your css folder inside view folder

var express = require('express')
var app = express();
var router = express.Router();
var path = __dirname + '/views/';
app.use(express.static(path.join(__dirname, 'views')));


router.get("/",function(req,res){
 res.sendFile("index.html");
});

app.use("/",router);

app.use("*",function(req,res){
 res.sendFile("index.html");
});

app.listen(8080,function(){
 console.log("Live at Port 8080");
});

Upvotes: 0

Simeon Nakov
Simeon Nakov

Reputation: 1048

If I understand the problem correctly, you don't seem to have set a folder for static files. Ex:

app.use(express.static(path.join(__dirname, 'static')));

After you make one it should start looking for the css in there. For example static/css/main.css can be used with <link href="css/main.css">

Upvotes: 1

LMokrane
LMokrane

Reputation: 826

I think you should use express's serving static files, as explained in their API : https://expressjs.com/en/starter/static-files.html

Upvotes: 1

Related Questions