【问题标题】:How do i stop access to my index.html file using node and ejs如何停止使用 node 和 ejs 访问我的 index.html 文件
【发布时间】:2021-07-25 13:20:09
【问题描述】:

如何停止对 index .html 文件的访问并让人们在 home.ejs 上注册,我正在构建一个静态网站,但现在正在制作一个 Web 应用程序并希望人们注册。

我注释掉了索引部分,但索引仍然首先出现而不是首页。

我希望人们先注册,然后再使用该应用

这是我的代码。

 //jshint esversion: 6
const path = require("path");
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");

const app = express();

app.use(express.static(path.join(__dirname, 'public')));

app.set('view engine', 'ejs');

app.use(bodyParser.urlencoded({extended:true}));

/* app.get("/", function(req, res){
    res.sendFile(path.join(__dirname, "public", "index.html"));
}); */

app.get("/home", function(req, res){
    res.render("home")
})

app.listen(3000);

console.log('now the server is running')

【问题讨论】:

  • 您使用来自publicstatic assets,其中包括您的index.html 文件。您可能不希望 index.html 作为静态资源

标签: node.js express ejs


【解决方案1】:

应用使用来自 public 的静态资产,其中包括您的 index.html 文件。

您可以从静态源中删除 index.html

app.get("/", function(req, res){
    res.sendFile(path.join(__dirname, "views", "index.html"));
});

或者您可以提供静态文件夹的基本路径。

app.use('/public' ,express.static(path.join(__dirname, 'public')));

【讨论】:

  • 哦,好的,兄弟,非常感谢您的帮助 :-)
猜你喜欢
  • 1970-01-01
  • 2014-08-26
  • 1970-01-01
  • 2022-01-10
  • 2018-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多