简体   繁体   English

在共享主机上部署 nodejs 文件

[英]Deploying nodejs files on shared hosting

  1. In PHP I used to place files in public_html folder.在 PHP 中,我曾经将文件放在 public_html 文件夹中。 I did the same for nodejs and started my application using "forever start app.js".我对 nodejs 做了同样的事情,并使用“永远启动 app.js”启动了我的应用程序。 Routers worked as expected, but I can able to see my nodejs source codes in the browser eg http://example.com/app.js showed the source code of app.js.路由器按预期工作,但我可以在浏览器中看到我的 nodejs 源代码,例如http://example.com/app.js显示了 app.js 的源代码。

  2. In some tutorials, They placed the files in /var/www/html/ and started the application.在一些教程中,他们将文件放在 /var/www/html/ 中并启动了应用程序。 What's the difference between public_html and /var/www/html? public_html 和 /var/www/html 有什么区别?

  3. I'm using shared hosting, so I don't have permissions to place the files in /var/www/html/.我使用的是共享主机,所以我无权将文件放在 /var/www/html/ 中。 I deployed nodejs files in /home/%username% folder and visited http://example.com/app.js .我在 /home/%username% 文件夹中部署了 nodejs 文件并访问了http://example.com/app.js This time source codes are not visible and router thrown 404 error page as expected.这次源代码不可见,路由器按预期抛出了 404 错误页面。 (Deleted files in public_html before deploying in /home/%username%) (在 /home/%username% 部署之前删除了 public_html 中的文件)

  4. The index page router didn't work as expected.索引页路由器没有按预期工作。 Instead of '/' or '/index', router received '/index.html.var' for the index page ( http://example.com/ ).路由器收到索引页( http://example.com/ )的“/index.html.var”而不是“/”或“/index”。

Please guide me on deploying nodejs app securely on shared hosting.请指导我在共享主机上安全地部署 nodejs 应用程序。

Er, no, Node.js is different, it's not PHP.呃,不,Node.js 不同,它不是 PHP。 A programme in Node.js is just like C/C++, Python, or any other general purpose programming language, it can control (likely) the whole server, so it's not to be deployed onto shared hosting. Node.js 中的程序就像 C/C++、Python 或任何其他通用编程语言一样,它可以控制(可能)整个服务器,因此它不会部署到共享主机上。

You will need a cheap VPS (virtual private server, cloud server) at least, very cheap nowadays, on a par with shared hosting.您至少需要一个便宜的 VPS(虚拟专用服务器、云服务器),现在非常便宜,与共享主机相当。

For starting, I suggest Heroku, free server: https://www.heroku.com首先,我建议 Heroku,免费服务器: https : //www.heroku.com

Programming notes: Node.js is server-side language, it doesn't run in browser like traditional JS, you won't visit it by URL, unless you create a webserver using Express.js or that kind of library.编程注意事项:Node.js 是服务器端语言,它不像传统的 JS 那样在浏览器中运行,你不会通过 URL 访问它,除非你使用 Express.js 或那种库创建一个网络服务器。

Example using Express.js to server static files in public_html :使用 Express.js 在 public_html 中服务器静态文件的示例

  • Put your server.js (or app.js, or whatever you put) outside public_html, don't put your Node.js code there.将您的 server.js(或 app.js,或任何您放置的)放在 public_html 之外,不要将您的 Node.js 代码放在那里。
  • For example when you put your app.js right outside public_html , serve static files this way:例如,当您将app.js放在public_html之外时,以这种方式提供静态文件:

    app.use('/static', express.static('public_html'))

  • Access your files at URL paths starting with /static , or use the following middleware use to server static files at root URL path.:在以/static开头的 URL 路径中访问您的文件,或使用以下中间件用于在根 URL 路径中服务器静态文件:

    app.use('/', express.static('public_html'))

Reference: http://expressjs.com/en/starter/static-files.html参考: http : //expressjs.com/en/starter/static-files.html

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

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