[英]Express.js server is not rendering static javascript file
I am building a website and using an express.js server to capture information submitted through a form.我正在构建一个网站并使用 express.js 服务器来捕获通过表单提交的信息。 Before setting up the server I already had the site built, using static js and css files.在设置服务器之前,我已经使用静态 js 和 css 文件构建了站点。 After connecting to the server, I was able to get the server to render the html and css stylesheet.连接到服务器后,我能够让服务器呈现 html 和 css 样式表。 But it won't show the vanilla js I had already coded.但它不会显示我已经编码的 vanilla js。 I only want to use the server for the form and have the vanilla javascript still render like before connecting to the server.我只想将服务器用于表单,并让 vanilla javascript 仍然像在连接到服务器之前一样呈现。
I have the files set up as:我将文件设置为:
root
index.html
app.js
public
css
styles.css
javascript
index.js
app.js应用程序.js
const express = require("express");
const https = require("https");
const path = require('path')
const app = express();
app.use(express.static(path.join(__dirname, '/public')));
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.get("/", function (req, res) {
res.sendFile(__dirname + "/index.html");
});
index.html索引.html
<link rel="stylesheet" href="css/styles.css" />
<script src="javascript/index.js"></script>
What am I doing wrong here?我在这里做错了什么? Is there something I need to code in my index.js file to get it to connect to the server?是否需要在我的 index.js 文件中编写代码才能使其连接到服务器? Thanks in advance.提前致谢。
app.js
1.在app.js
添加这个中间件 app.use(express.static(path.join(__dirname, "public")));
|__public/
|__ css/
|__ css files...
|__ js/
|__ js files...
3. Import this way 3.以这种方式导入
Now you set the path to the public directory you have to give the path public folder when you import现在您将路径设置为公共目录,您必须在导入时提供路径公共文件夹
<link rel="stylesheet" href="/css/main.css" />
<script src="/js/main.js" crossorigin="anonymous"> </script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.