【问题标题】:how to include static files (html ,css) in express.js如何在 express.js 中包含静态文件(html、css)
【发布时间】:2022-07-12 01:27:16
【问题描述】:

我无法在 express 应用中包含静态 CSS 文件

我已经在输出文件 CSS 中使用了 app.use(express.static('public') 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");
});

【问题讨论】:

标签: node.js express static


【解决方案1】:

好吧,您正确地包含了静态文件,但是您的文件夹结构是什么样的?既然你叫app.use(express.static("public"));你的css应该在一个名为“public”的文件夹中,我也可以看看你的html吗?你仍然需要在你的 html 文件中链接你的 css,你知道吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 2012-01-24
    • 2019-04-27
    • 1970-01-01
    相关资源
    最近更新 更多