[英]How to run/view ExpressJS server for webpages other than index.html?
So, I want to view/run/display webpages other than the index.html from my public folder which has multiple html files using ExpressJS and NodeJS.所以,我想从我的公共文件夹中查看/运行/显示除 index.html 以外的网页,该文件夹有多个使用 ExpressJS 和 NodeJS 的 html 文件。 Every time I run my server, I can only view the index.html file.
每次运行我的服务器,我只能查看 index.html 文件。 Is there a way I can access other html files?
有没有办法可以访问其他 html 文件? I am a beginner and just getting started with the backend part.
我是初学者,刚刚开始使用后端部分。 This is my app.js
这是我的 app.js
app=express();
const path=require('path');
const Router=express.Router();
const port=process.env.PORT||3000;
require("./db/connectdb");
const static_path=path.join(__dirname,"../../frontend/public");
app.use(express.static(static_path));
app.get('/createelection',(req,res)=>{
console.log("Create an Election here");
});
app.listen(port,()=>{
console.log('Server is running at port no. '+ port);
});
My Public Folder我的公共文件夹
Public
-index.html
-createelection.html
-voterlogin.html
From a comment on the question:从对该问题的评论中:
localhost:3000/createelection
By default the static module will:默认情况下,static 模块将:
index.html
if you ask for a path ending in /
/
结尾的路径,请给您index.html
You are asking for createelection
but the file is named createelection.html
.您要求
createelection
但文件名为createelection.html
。
With your current code you need to ask for http://localhost:3000/createelection.html
.使用您当前的代码,您需要询问
http://localhost:3000/createelection.html
。
Alternatively you can tell Express to try to autocomplete the file extension for you.或者,您可以告诉 Express 尝试为您自动完成文件扩展名。 Look at the documentation for the static module :
查看static 模块的文档:
extensions : Sets file extension fallbacks: If a file is not found, search for files with the specified extensions and serve the first one found.
extensions :设置文件扩展名后备:如果找不到文件,则搜索具有指定扩展名的文件并提供第一个找到的文件。 Example:
['html', 'htm']
.示例:
['html', 'htm']
。
The setting defaults to false
该设置默认为
false
So you would need:所以你需要:
app.use(express.static(static_path, { extensions: true }));
You can use routes
.您可以使用
routes
。 Routes allow you to enter a path in the browser and the express server will handle the request and output your file, instead of having to enter an absolute path.路由允许您在浏览器中输入路径,快速服务器将处理请求和 output 您的文件,而不必输入绝对路径。
Example:例子:
const express = require("express");
const app = express();
const path = require("path");
const port = process.env.PORT || 3000;
require("./db/connectdb"); // Connect to the database
// Define a static path
const static_path = path.join(__dirname, "../../frontend/public");
// Send all static files to the server
app.use(express.static(static_path));
// Routes
app.get("/", (req,res) => {
res.sendFile(path.join(static_path, "/index.html");
});
app.get("/create-electron", (req,res) => {
res.sendFile(path.join(static_path, "/createelectron.html");
});
app.get("/login", (req,res) => {
res.sendFile(path.join(static_path, "/voterlogin.html");
});
// Run app on a specific port
app.listen(port,() => {
console.log("Server is running at port no. " + port);
});
After running the code above, if you enter localhost:3000
into your browser, your index.html
file will be displayed, if you enter localhost:3000/create-electron
you will see createelectron.html
, etc.运行上面的代码后,如果你在浏览器中输入
localhost:3000
,就会显示你的index.html
文件,如果你输入localhost:3000/create-electron
你会看到createelectron.html
等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.