简体   繁体   中英

How can I deploy a Node.js application?

Node.js is "JavaScript on the server". OK. But, how can I "deploy" a Node.js application? Which kind of web server should I use? 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 .

Regarding "which kind of web server" you should use, it depends on what you're trying to build. 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. If you need MySQL, the most popular one right now seems to be 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. There are several good articles here , for example. From there you can move into web servers and database drivers more comfortably.

There are many deployment solutions available, CloudFoundry being one of them. I think you need a great understanding of how Node works first though. 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. If your application needs a web server, there are some solutions in Node like Express .

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.

You should start by looking at 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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