简体   繁体   English

如何为 index.html 以外的网页运行/查看 ExpressJS 服务器?

[英]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 模块将:

  • Give you index.html if you ask for a path ending in /如果您要求以/结尾的路径,请给您index.html
  • Give you the file you ask for给你你要的文件

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.

相关问题 ExpressJs不提供index.html - ExpressJs is not serving index.html ExpressJS 重定向索引.html - ExpressJS redirect index.html Express.js-在index.html中简单查找和替换 - Expressjs - simple find and replace in index.html 使用build.phonegap.com-如何在除index.html之外的页面上引用cordova.js? - using build.phonegap.com - how to reference cordova.js on pages other than index.html? 当 webpack 服务器运行时,如何运行不同的 html 文件而不是 index.html? - how to run a different html file instead of index.html when webpack server runs? 我如何在没有Index.html或.php的情况下将WebApplication文件运行到服务器? - How can I run my WebApplication files to the server without an Index.html or.php? 如何在没有任何本地服务器的情况下通过单击 index.html 文件来运行 React 应用程序 - How to run React application by clicking index.html file without any local server 如何使用 index.php 而不是 index.html 在 XAMP 上运行由 create-react-app 创建的 ReactApp? - How to run ReactApp created by create-react-app on XAMP using index.php rather than index.html? 如果没有 index.html,如何在 index.html 中运行脚本 - How to run scripts in index.html in react if you don't have index.html 寻找比index.html更好的索引 - Looking for a better index than index.html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM