【问题标题】:Adding another page other than index.html添加 index.html 以外的其他页面
【发布时间】:2019-02-21 14:46:34
【问题描述】:

我启动了自己的 VPS,并使用 NGINX 启动了一个 Web 服务器。我有一个 index.html。如何创建其他页面,例如关于页面,并将其放在 www.my-domain-name.com/about/

这是否意味着我必须编辑我的 app.js 文件,如果是,如何编辑?

修正:我在 Express 代码中添加了 Lazar 建议的修正以获取 about.html。

'use strict';

const express = require("express");
const app = express();

// Static css/js files
app.use('/static', express.static('./dist'));

app.get("/", function(req, res) {
  res.sendFile( __dirname + '/index.html');
});

app.get("/about", function(req, res) {
    res.sendFile( __dirname + '/about.html');
});

const port = 3001;

// Start server
app.listen(port, function() {
  console.log("Listening on " + port);
});

在index.html中,about页面的链接是:

<a href="/about.html">About me.</a>

/about/about.html 目前都不起作用并收到错误消息:Cannot GET /about.html

编辑:我使用的是forever,所以我必须forever restartall

【问题讨论】:

    标签: html express nginx forever


    【解决方案1】:

    考虑到您正在使用 express,为每个创建的路线创建适当的 html 页面 - 或使用其他东西,如把手等。

    例如,您为“/”路由创建了 index.html。 对于 about us 路由,创建 aboutus.html

    app.get("/aboutus", function(req, res) {
      res.sendFile( __dirname + '/aboutus.html');
    });
    

    等等……

    欲了解更多信息,请查看他们的官方网页:https://expressjs.com/

    【讨论】:

    • 我在 app.js 中添加了 about 路由和页面链接(

      关于我。

      ),但它仍然给我错误:Cannot GET /about.html
    • 我认为为了让我们帮助您,您应该将更多代码放在这里。向我们展示 index.html,但现在,请尝试

      关于我。

    • 我与朋友确认,Lazar 的解决方案有效!我忘记做的是“永远重启”[我正在使用“永远”]
    猜你喜欢
    • 2015-02-04
    • 2017-12-29
    • 2018-10-18
    • 2020-12-22
    • 2015-02-05
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多