简体   繁体   English

如何部署Node.js应用程序?

[英]How can I deploy a Node.js application?

Node.js is "JavaScript on the server". Node.js是“服务器上的JavaScript”。 OK. 好。 But, how can I "deploy" a Node.js application? 但是,我如何“部署”Node.js应用程序? Which kind of web server should I use? 我应该使用哪种Web服务器? How can I create "Controllers"? 如何创建“控制器”? And how can I persist data in a DB? 我如何在数据库中保存数据? Thanks in advance. 提前致谢。

This is one of the best places to get familiar with what's currently available: https://github.com/joyent/node/wiki/modules . 这是熟悉当前可用内容的最佳地点之一: https//github.com/joyent/node/wiki/modules

Regarding "which kind of web server" you should use, it depends on what you're trying to build. 关于“你应该使用哪种Web服务器”,这取决于你要构建的内容。 I currently use express , which I've been very happy with. 我目前使用快递 ,我一直非常满意。

For database connectivity, that depends on what type of database you're connecting to. 对于数据库连接,这取决于您要连接的数据库类型。 For MongoDB, I use Mongoose , and for CouchDB I just use a raw HTTP client. 对于MongoDB,我使用Mongoose ,而对于CouchDB,我只使用原始HTTP客户端。 If you need MySQL, the most popular one right now seems to be node-mysql . 如果你需要MySQL,现在最流行的似乎是node-mysql There are lots of other database drivers here . 有很多其他的数据库驱动程序的位置

Given the high-level nature of your question, it sounds like you might be better profited by some "getting started" guides, to really get familiar with what node.js is. 鉴于您的问题的高级性质,听起来您可能会通过一些“入门”指南获得更好的利润,以真正熟悉node.js。 There are several good articles here , for example. 有几个很好的文章在这里 ,例如。 From there you can move into web servers and database drivers more comfortably. 从那里,您可以更舒适地进入Web服务器和数据库驱动程序。

There are many deployment solutions available, CloudFoundry being one of them. 有许多可用的部署解决方案, CloudFoundry就是其中之一。 I think you need a great understanding of how Node works first though. 我认为你需要对Node的工作方式有一个很好的理解。 Basically, to "deploy" an application, you would typically send the files over to the server and run it from the commandline: 基本上,要“部署”应用程序,通常会将文件发送到服务器并从命令行运行它:

node server.js

There is no web server involved like Apache or nginx. 没有涉及像Apache或nginx这样的Web服务器。 If your application needs a web server, there are some solutions in Node like Express . 如果你的应用需要一个Web服务器,也有类似节点的一些解决方案

Databases work as usual. 数据库像往常一样工作。 You install one over to your server, use one of the many Node modules to connect to it and write data. 您将一个安装到您的服务器,使用众多节点模块之一连接到它并写入数据。 It's separate from your Node server. 它与您的节点服务器分开。

Check out this excellent list of Node modules from the GitHub wiki. 从GitHub wiki中查看这个优秀的Node模块列表

You should start by looking at http://nodejs.org . 你应该从http://nodejs.org开始。 Great place to find information when writing code. 编写代码时查找信息的好地方。 There are a lot of modules that you can use or you can start from scratch and work your way up. 你可以使用很多模块,或者你可以从头开始工作。

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');

Easiest example of an web server written in node. 在节点中编写的Web服务器的最简单示例。

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

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