简体   繁体   English

如何使用Nginx通过Express路由器加载页面

[英]How to use Nginx to load pages through express router

So I'm building an end to end application (With node.js/mysql back end, react front end, and using the express router), but I'm having trouble setting up a local development server. 因此,我正在构建一个端到端应用程序(使用node.js / mysql后端,响应前端并使用Express路由器),但是在设置本地开发服务器时遇到了麻烦。 I don't need it to be accessed from the outside world, just be able to load different pages connecting to the express router. 我不需要从外部访问它,只需能够加载连接到快速路由器的不同页面即可。 I don't have any dev ops experience for this, so I'm trying to use nginx to point it to the router which I can't figure out. 我对此没有任何开发经验,因此我尝试使用nginx将其指向我不知道的路由器。 Is there an easier way to do this? 有没有更简单的方法可以做到这一点?

I also need to run this on a windows machine, which just makes everything slightly more complicated 我还需要在Windows机器上运行它,这只会使所有事情变得稍微复杂一些

You'll probably want to use https://github.com/facebook/create-react-app 您可能需要使用https://github.com/facebook/create-react-app

create-react-app my-app will set up everything you need (webpack, etc.), and then npm start will start a local development server. create-react-app my-app将设置您需要的所有内容(webpack等),然后npm start将启动本地开发服务器。

Should work on Windows, but I don't know, because I wouldn't use/recommend Windows ;-) 应该可以在Windows上运行,但是我不知道,因为我不会使用/推荐Windows ;-)

It's not entirely clear from your description how your application is set up and what the role of Nginx is. 从您的描述中还不能完全清楚您的应用程序是如何设置的以及Nginx的作用是什么。

So I'll start from the beginning... 所以我将从头开始...

Nginx is primarily an HTTP server which can also function as a proxy for HTTP requests. Nginx主要是一个HTTP服务器,它也可以充当HTTP请求的代理。 If you've written a Node.js application using Express, you have written an HTTP server which can handle any routes you have set up and can also serve your static assets (ie. HTML pages, images, front-end Javascript, CSS, etc.). 如果您使用Express编写了Node.js应用程序,则您编写了一个HTTP服务器 ,该服务器可以处理您设置的任何路由,还可以提供您的静态资产(例如HTML页面,图像,前端Javascript,CSS,等等。)。 In this case, there is no need for Nginx - if you wrote something like the Express "Hello World" app , then you will see a message like "Example app listening on port 3000" and you can connect to your app by visiting http://localhost:3000 in your browser. 在这种情况下,不需要Nginx-如果您编写了Express Express“ Hello World”应用程序之类的内容 ,则将看到诸如“示例应用程序在端口3000上监听”之类的消息,并且可以通过访问http://localhost:3000连接到您的应用程序http://localhost:3000浏览器中的http://localhost:3000

That's it - there's literally nothing else to your app and there is no need for Nginx (or any other HTTP server) to run your application. 就是这样-您的应用程序几乎没有其他东西,也不需要Nginx(或任何其他HTTP服务器)来运行您的应用程序。

Now that's not to say that there is no role for Nginx in your application, but it may not be as an HTTP server. 现在,这并不是说Nginx在您的应用程序中没有作用,但它可能不是HTTP服务器。 One possibility is that you may want to set up Nginx as a proxy, to handle certain routes by sending the requests to your Node application. 一种可能是您可能希望将Nginx设置为代理,以通过将请求发送到Node应用程序来处理某些路由。 For example, I set up an application some time ago which uses Nginx to proxy API routes for my application to a Node application and to serve static assets directly. 例如,我前段时间建立了一个应用程序,该应用程序使用Nginx将我的应用程序的API路由代理到Node应用程序,并直接提供静态资产。 This may be what you have in mind - if it is, you will need to configure different routes in Nginx to serve different things (and unfortunately there's not enough information in your question to give suggestions on this). 这可能就是您要记住的-如果是这样,您将需要在Nginx中配置不同的路由来服务不同的内容(不幸的是,您的问题中没有足够的信息来提供建议)。

As an aside, you're probably going to find this much easier to set up using Linux - perhaps the Windows Linux Subsystem, a virtual machine running Linux, or Docker. 顺便说一句,您可能会发现使用Linux设置起来容易得多-也许是Windows Linux子系统,运行Linux的虚拟机或Docker。

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

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