expressjs_使用ExpressJS交付HTML文件

expressjs

In Node.js and ExpressJS applications, there used to be a very simple way to deliver an HTML file or any other sort of file: res.sendfile(). Delivering HTML files using Express helps make development quick and easy when you need a quick HTTP server.

在Node.js和ExpressJS应用程序中,曾经有一种非常简单的方式来交付HTML文件或任何其他类型的文件: res.sendfile() 。 当您需要快速的HTTP服务器时,使用Express交付HTML文件可帮助您快速轻松地进行开发。

Recently, this function has become deprecated by ExpressJS and if you try using the function, you'll get an error saying that function is deprecated and you should use res.sendFile().

最近,ExpressJS已弃用该函数,如果尝试使用该函数,则会收到一条错误消息,指出该函数已弃用,应使用res.sendFile()

使用res.sendFile() (Using res.sendFile())

To use res.sendFile, we will need to pass in a path to the file.

要使用res.sendFile ,我们需要传递文件路径。

We will also need to pull in the built-in path module so that we can link to the file.

我们还需要引入内置路径模块,以便我们可以链接到文件。

Here is an example of using res.sendFile() to deliver an HTML page.

这是使用res.sendFile()交付HTML页面的示例。

var express = require('express');
var app = express();
var path = require('path');

// viewed at http://localhost:8080
app.get('/', function(req, res) {
    res.sendFile(path.join(__dirname + '/index.html'));
});

app.listen(8080);

样例代码 (Sample Code)

We'll need a Node application to start. So you can create one by running the following commands:

我们需要一个Node应用程序来启动。 因此,您可以通过运行以下命令来创建一个:

$ mkdir express-sendfile
$ cd sendfile
$ npm init
$ npm install express --save
$ touch server.js index.html

Now we have the foundation for our quick Node app. server.js will be the file that will contain the route to serve our index.html file.

现在,我们为快速的Node应用程序奠定了基础。 server.js将是包含服务于index.html文件的路由的文件。

server.js (server.js)

Here's the code for that:

这是代码:

var express = require('express');
var app = express();
var path = require('path');

// viewed at http://localhost:8080
app.get('/', function(req, res) {
    res.sendFile(path.join(__dirname + '/index.html'));
});

app.listen(8080);

index.html (index.html)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Sample Site</title>

    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
    <style>
        body { padding-top:50px; }
    </style>
</head>
<body>

    <div class="container">
        <div class="jumbotron">
            <h1>res.sendFile() Works!</h1>
        </div>
    </div>

</body>
</html>

Now after our server has been started using:

现在,我们的服务器已开始使用:

$ node server.js

We can see our site in our browser!

我们可以在浏览器中看到我们的网站!

express-sendfile

结论 (Conclusion)

res.sendFile() is a very easy function to use. Express provides a great many tools for Node users and we can even use this to deliver downloadable files or any files really.

res.sendFile()是一个非常易于使用的函数。 Express为Node用户提供了很多工具,我们甚至可以使用它来交付可下载文件或实际上的任何文件。

翻译自: https://scotch.io/tutorials/use-expressjs-to-deliver-html-files

expressjs

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值