简体   繁体   中英

how to include static files (html ,css) in express.js

i am not able to include static CSS file in the express app

I have used app.use(express.static('public') still in the output file CSS in not include

 // initialize modules // ATHARVA BHAVSAR const express = require("express"); const bodyParser = require("body-parser"); const request = require("request"); //creating app from express method const app = express(); //body parser app.use(bodyParser.urlencoded({ extended: true })); //including static files in the app app.use(express.static("public")); //get method for the app app.get("/", (req, res) => { res.sendFile(__dirname + "/signup.html"); }); //post method for the app app.post("/", (req, res) => { //getting the entries from the user const FirstName = req.body.Fname; const LastName = req.body.Lname; const Email = req.body.email; //trial console.log(FirstName, LastName, Email); }); //listening to port 3000 app.listen(3000, function () { console.log("Server is running on port 3000"); });

Well, you're including the static files correctly, but how are your folder structure looking like? since you called app.use(express.static("public")); your css should be inside a folder called "public", also can I take a look at your html? You still need to link your css in your html file, you know right?

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