【问题标题】:I would like to get by node.js app to serve up multiple html files我想通过 node.js 应用程序来提供多个 html 文件
【发布时间】:2023-03-26 04:48:01
【问题描述】:

所以现在我有以下代码可以让“index.html”正确显示在: http://staging-supermodels.herokuapp.com/

但我的问题是如何更改它,以便在用户单击“登录”按钮时也显示我的其他文件(如 SignIn.html)?谢谢!

var express = require('express');
var fs = require('fs');

var app = express.createServer(express.logger());

var html_buffer = new Buffer(fs.readFileSync('index.html', 'utf-8'), 'utf-8');
var html_lines = html_buffer.toString("utf-8");

// public directory stores .css files
app.use(express.static(__dirname + '/css_files'));

// handler for dynamic content(webpages that change over time)
app.get('/', function(request, response) {
  response.send(html_lines);
});

var port = process.env.PORT || 8080;
app.listen(port, function() {
  console.log("Listening on " + port);
});

【问题讨论】:

    标签: html node.js web-applications


    【解决方案1】:

    在您发布的代码中,您实际上有答案,app.get()。为了提供 HTML 文件(或任何文件),客户端浏览器必须“获取”该文件,在本例中为 HTML 文件。

    app.get('/Signin', function(request, response){
     response.send(''); // you will put path to your html file within the "('')"
    });
    

    【讨论】:

    • 因此,如果我的 SignIn.html 和 index.html 与我的 web.js 文件与我在问题中显示的代码位于同一个文件夹中,这看起来会如何。谢谢,但我只是想在尝试之前绝对确定。
    • 'app.get('/', function(request, response){ response.send('index') //如果这不起作用试试'index.html' };' app.get('/signin', function(request, response){ response.send('signing') // 如果这不起作用尝试 'signing.html' }; 如果这些不起作用,请告诉我我再研究一下,对传统的node.js有点生疏了,我一般用express和jade,绝对值得研究。
    猜你喜欢
    • 2019-05-26
    • 2017-04-17
    • 2015-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 2018-04-24
    • 1970-01-01
    相关资源
    最近更新 更多