简体   繁体   English

将现有Express节点应用发布到静态站点的最简单方法?

[英]Easiest way to publish existing Express node app to a static site?

I've been incredibly spoiled by working almost exclusively with React via create-react-app which has a build utility for npm which makes deploying my projects to static web hosting super easy (github pages / surge) and in the end just gives me raw html / css / js. 我几乎无法通过使用create-react-appReact专门合作来宠坏,该应用程序具有适用于npm的构建实用程序,这使得将我的项目部署到静态Web托管变得非常容易(github页面/ surge),最终给了我原始的印象。 html / css / js。

But recently a friend approached me, in whatever school he's in they've taught him a ton about how to write apps with just Express but nothing about how to deploy them. 但是最近有一个朋友走近我,无论他在哪所学校里,他们都教给他很多关于如何仅使用Express编写应用程序的知识,却没有关于如何部署它们的知识。 I've got his code up on Heroku for the time being but he'd probably be better off with just static hosting (since the site is incredibly simple and only uses express for routing / features no server side code or interaction) 我暂时在Heroku上编写了他的代码,但仅使用静态托管可能会更好(因为该站点非常简单,并且仅使用express进行路由/没有服务器端代码或交互功能)

Is there anything like create-react-app's build functionality for express? 是否有诸如create-react-app的快速构建功能? There's no way this incredibly simple thing absolutely needs the overhead of a full node server. 如此简单的事情绝对不可能完全占用整个节点服务器的开销。

I think you may be conflating a few things, so let's start at the beginning just to make sure we're using the same definitions. 我想您可能会混淆一些事情,所以让我们从头开始只是为了确保我们使用的是相同的定义。

Node is a JavaScript-engine, powered by the V8 engine (which was built for Chrome). Node是一个JavaScript引擎,由V8引擎(专为Chrome构建)提供支持。 It's basically a JavaScript interpreter. 它基本上是一个JavaScript解释器。

Express is a package, built on top of Node, which builds upon Node's http library. Express是一个在Node之上构建的程序包,它基于Node的http库构建。 It's (one of) Node's equivalents to nginx or apache . 它是Node等效于nginxapache的(其中之一)。

Heroku is a cloud-based web host, which has the ability to scale dynamically (like AWS, which it is built on top of). Heroku是基于云的Web主机,具有动态扩展的能力(就像AWS一样,它是基于它构建的)。

To say "Express doesn't need a full-blown Node server" doesn't really make much sense. 说“ Express不需要成熟的Node服务器”并没有多大意义。 He just needs a server which can run Node apps. 他只需要一台可以运行Node应用程序的服务器即可。 The size of the Node server can vary, and indeed he probably doesn't need a very big one. Node服务器的大小可以变化,实际上他可能不需要一个很大的服务器。

Heroku is probably a very suitable host. Heroku可能是一个非常合适的主机。 He doesn't have to use scaling or anything and it is fairly easy to deploy a Node app there. 他不必使用缩放比例或任何东西,并且在那里轻松部署Node应用程序非常容易。

Many other popular hosts can also support Node. 许多其他流行的主机也可以支持Node。 If they give you terminal access and the ability to install applications, they almost certainly can. 如果它们使您能够访问终端并能够安装应用程序,则几乎可以肯定。

As far as I know, there aren't any programs like create-react-app which will just spin up a new instance of an express app. 据我所知,没有像create-react-app这样create-react-app会启动express应用程序的新实例。 That's probably because they're insanely simple to setup. 那可能是因为它们非常容易设置。 Just two files are sufficient for a bare-bones express app: package.json and your express file (I'll name it index.js . 对于一个基本的Express应用程序,只有两个文件就足够了: package.json和您的express文件(我将其命名为index.js

package.json: package.json:

{
   "name": "my-app",
   "dependencies": {
     "express": "latest"
   }
}

index.json: index.json:

const express = require('express');

const app = express();

app.get('/', (req, res) => res.send('<h1>Hello World</h1>').end());

app.listen(8888, () => console.log('Listening on port 8888'));

Deploying that to any server that can run Node and running the command npm install; node index.js 将其部署到可以运行Node的任何服务器上,并运行命令npm install; node index.js npm install; node index.js will start it running. npm install; node index.js将使其开始运行。

Very frequently, you'll actually create an express server to serve up your web app, frequently written in React (this is the scenario I work in every day). 通常,您实际上会创建一个express服务器来服务您的Web应用程序,该服务器经常以React编写(这是我每天工作的场景)。 The create-react-app is just a quick way to set up a project that is rather opinionated in its structure, but is not the only way to use React. create-react-app只是建立项目的一种快速方法,该项目在结构上颇有建树,但不是使用React的唯一方法。

(Super technically, you can get away without package.json , but you'd have to manually run npm install express on the server, and that'd be really weird, so don't do that). (从技术上讲,不需要package.json可以逃脱,但是必须在服务器上手动运行npm install express ,这确实很奇怪,所以不要这样做)。

I had the same feeling working on very simple site, so I've transferred my express application into web-boost approach which is using the same express under the hood. 我在非常简单的网站上工作时也有相同的感觉,因此我已将express应用程序转换为Web-boost方法,该方法在后台使用相同的Express。

The only thing you have to do is create web-boost.json config file which looks like 您唯一要做的就是创建看起来像这样的web-boost.json配置文件

{
  "routes": {
    "/": {
      "view": "index.twig",
      "vars": {
        "title": "Home page",
        "greeting": "Hello world!"
      },
      "assets": {
        "js/index.min.js": [
          "js/index.js"
        ]
      }
    },
    "/user": {
      "view": "user.twig",
      "vars": {
        "title": "User's page",
        "greeting": "Hi John Doe!"
      },
      "assets": {
        "css/user.min.css": [
          "styles/bootstrap.min.css",
          "styles/user.scss"
        ]
      }
    }
  }
}

and then use wb-compile CLI command to convert your dynamic application into static one. 然后使用wb-compile CLI命令将动态应用程序转换为静态应用程序。

PS After that I've just deployed static pages to AWS.S3 PS之后,我刚刚将静态页面部署到了AWS.S3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM