原文寫得太好了,awesome!!! 網上看到的大部分漢語寫得文章都有各種各樣的問題,轉載如下:
SET UP THE FULL STACK AND HAVE A WEBPAGE RUNNING IN 30 MINUTES. MAKE IT TALK TO YOUR DB IN ANOTHER 30.
By Christopher Buecheler
NEW: Updated for ExpressJS v4!
You can find/fork the entire sample project on GitHub INTRODUCTION
There are approximately one hundred million tutorials on the web for getting a "Hello, World!" app running with Node.js. This is great! It's especially great if your goal is to greet the world and then give up on your web career and go spend the rest of your life as, like, a jockey or something. That doesn't really describe most of us, so we go looking for more tutorials.
In my experience, the "next level" tutorials out there seem about 30 levels further along. We go from "Hello, World!" to building out an entire blogging system with comments. Which is also great, but a lot of times those tutorials assume the reader has done a whole bunch of intermediate fiddling, and they often drop a bunch of big functions on you all at once. I tend to learn best by making lots of smaller, intermediate steps, and I don't think I'm the only one.
I'm not the only one, right?
Well, good news, everyone! I've done the fiddling and read a bunch of tutorials and shouted at my command prompt until things finally worked. I have a web project up and running which uses Node.JS, the Express framework, the Jade HTML pre-processor, and MongoDB for data. I can read to and write from the DB. From there, the sky's the limit.
Here's the deal: I'm going to show you how to get all of this stuff set up. I'll be assuming that you're a front-end developer who knows HTML5/CSS3/JavaScript well enough that I don't have to explain those. If that's you, then this should be a solid primer.
Your app will look pretty, it will connect to a DB, it'll get some results, and it'll do stuff with those results. Then for kicks we'll also make it save data to the DB. Through it all, I will explain what the code does, and how to write it, instead of just giving you massive functions to stare at. We'll go from nothing even installed, to a DB-driven web app written in a language you fully understand, and the foundation necessary to build additional functionality into your app. And we'll do it in about 60 minutes of installation and coding time. Is that awesome? I submit that it is.
Let's go. PART I – 15 MINUTES OF INSTALLING
If you're really starting from scratch, then getting everything up and running takes a little bit of time. None of it is difficult. I run Windows 8 on my main machine, so it'll be slightly different for those on a Mac or Ubuntu or other *nix system, but it's principally the same thing in all cases. STEP 1 – INSTALL NODE.JS
This is really easy. Hit the Node.js website and click the big green Install button. It'll detect your OS and give you the appropriate installer (if for some reason it doesn't, click the downloads button and grab the one you need). Run the installer. That's it, you have installed Node.js and, equally important, NPM – Node Package Manager – which lets you add all kinds of great stuff to Node quickly and easily. Open a command prompt cd to the directory in which you wish to keep your test apps
(for the purposes of this tutorial, C:\node). STEP 2 – INSTALL EXPRESS
Now that we have Node running, we need the rest of the stuff we're going to actually use to create a working website. To do that we're going to install Express, which is a framework that takes Node from a barebones application and turns it into something that behaves more like the web servers we're all used to working with (and actually quite a bit more than that). We need to start with Express, because we're going to use its scaffolding to get the rest of what we need (more on that in a second). So let's type this: COMMAND C:\NODE\
C:\node>npm install -g express
This installs some core Express functionality right into our Node installation, making it available globally so we can use it anywhere we want. That's handy. You'll see a bunch of text in your command prompt, mostly a lot of http 304's and GETs. That's fine. Express is now installed and available.
New: as of Express 4.0, you'll need to install the express "generator" as well. This is following a trend in the node industry of breaking out core functionality from site-scaffolding utility (check out Yeoman sometime if you want to learn a whole lot more about scaffolding options out there). Installing the generator is very easy: in the same console you used to install Express globally, type the following: COMMAND C:\NODE>
C:\node>npm install -g express-generator
The generator should auto-install, and since it (like all packages installed with -g) lives in your master NPM installation directory, it should already be available in your system path. So let's use our generator to create the scaffolding for a website. STEP 3 – CREATE AN EXPRESS PROJECT
We're going to use Express and Jade, but not the Stylus CSS preprocessor (which people often use in this stack). We're just going to use straight CSS for right now. We have to use Jade or another templating engine to gain access to our Node/Express-based data. Jade's not hard to learn if you already know HTML. Just remember that you really have to pay attention to indentation or things will go badly wrong. A quick note on that: everything is this tutorial has been normalized to 4-space tabs, even code that was auto-generated with 2-space tabs. If you want to use two or three spaces, or actual tabs (which is usually my preference), that's just fine by me.
Anyway, still in c:\node or wherever you're storing your node apps, type this: COMMAND C:\NODE\
C:\node>express nodetest1
Hit enter and watch it go. You'll see something like this: COMMAND C:\NODE\
C:\node>express nodetest1 create : nodetest1 create : nodetest1/package.json create : nodetest1/app.js create : nodetest1/public create : nodetest1/public/javascripts create : nodetest1/public/images create : nodetest1/routes create : nodetest1/routes/index.js create : nodetest1/routes/users.js create : nodetest1/public/stylesheets create : nodetest1/public/stylesheets/style.css create : nodetest1/views create : nodetest1/views/index.jade create : nodetest1/views/layout.jade create : nodetest1/views/error.jade create : nodetest1/bin create : nodetest1/bin/www install dependencies: $ cd nodetest1 && npm install run the app: $ DEBUG=my-application ./bin/www
STEP 4 – EDIT DEPENDENCIES
OK, now we have some basic structure in there, but we're not quite done. You'll note that the express installation routine created a file called package.json in your nodetest1 directory. Open this up in a text editor and it'll look like this: C:\NODE\NODETEST1\PACKAGE.JSON
{ "name": "application-name", "version": "0.0.1", "private": true, "scripts": { "start": "node ./bin/www" }, "dependencies": { "express": "~4.0.0", "serve-favicon": "~2.1.3", "morgan": "~1.0.0", "cookie-parser": "~1.0.1", "body-parser": "~1.0.0", "debug": "~0.7.4", "jade": "~1.3.0" }}
This is a basic JSON file describing our app and its dependencies. We need to add a few things to it. Specifically, calls for MongoDB and Monk. Let's make our dependencies object look like this:
"dependencies": { "express": "~4.0.0", "serve-favicon": "~2.1.3", "morgan": "~1.0.0", "cookie-parser": "~1.0.1", "body-parser": "~1.0.0", "debug": "~0.7.4", "jade": "~1.3.0", "mongodb": "*", "monk": "*"}
STEP 5 – INSTALL DEPENDENCIES
Now we've defined our dependencies and we're rea