Getting up and RUNNING with NODE. JS, EXPRESS, JADE, and MONGODB

Source: Internet
Author: User
Tags generator mongodb install node css preprocessor

The original is very good, awesome!!! Most of the Chinese written articles on the internet have all kinds of problems, reproduced as follows:

The SET up is the full STACK and HAVE A webpage RUNNING in MINUTES. Make IT TALK to YOUR DB in ANOTHER.

by Christopher Buecheler

new:updated for Expressjs v4!

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 are 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's 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 levels further. We are "Hello, world!" to building out a entire blogging system with comments. Which is also great, but a lot the times those tutorials assume the reader has do a whole bunch of intermediate, And they often drop a bunch of the big functions on, at once. I tend to learn best by making lots of smaller, intermediate steps, and I don ' t i ' i ' m the only one.

I ' m not the one, right?

Good news, everyone!. I ' ve done the fiddling and read a bunch of tutorials and shouted in 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 fo R 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 and the all of this stuff set up. I'll be assuming so you ' re a front-end developer who knows Html5/css3/javascript and enough that I don ' t have to Explai N those. If that's you, the then this should to be a solid primer.

Your app'll look pretty, it'll connect to a DB, it's 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'll explain what the code does, and how to write it, instead of just giving your massive functions to St are at. We'll go even installed, to a Db-driven Web apps written in a language you fully understand, and the Foundatio N necessary to build additional functionality into your app. And we ' ll do it in about minutes of installation and coding. Is that awesome? I submit that it is.

Let ' s go. Part i–15 MINUTES of installing

If you are really starting from scratch, then getting everything the up and running takes a little bit of time. None of it is difficult. I Run Windows 8 on me 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 are really easy. Hit the Node.js website and click the Big Green Install button. It ' ll detect your OS and give you 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 your add all kinds of Great stuff to Node quickly and easily. Open a command prompt CD to the directory of which you wish to keep your test apps
(For the purposes of this tutorial, C:\node). Step 2–install EXPRESS

Now so we have Node running, we need the rest of the stuff we ' re going to actually with the use to create a working website. To do so we ' re going to install Express, which are a framework that takes Node from a barebones application and turns it into something which behaves more like the Web servers we ' re all used to working with (and actually quite a bit more than t HAT). We need to-start with Express, because we ' re going-to-use it scaffolding to-get the rest of what we need a second). So let ' s type this: COMMAND C:\NODE\

C:\NODE>NPM Install-g Express

This installs some core Express functionality to our Node installation, making it available globally so we can use It anywhere we want. That ' s handy. You ll have a bunch of the text in your command prompt, mostly a lot of HTTP 304 ' s and GETs. That ' s fine. The Express is now installed and available.

New:as of Express 4.0, you'll need to install the Express "generator" as OK.  This is following a trend in the node industry of breaking out core functionality from Site-scaffolding utility (check out Yeoman sometime if you are want to learn a whole lot other about scaffolding options out there). Installing the generator is very easy:in the same console your 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 installatio n directory, it should already is available in your system path. We generator to create the scaffolding for a website. Step 3–create a EXPRESS PROJECT

We ' re going to use Express and Jade, but is not the Stylus CSS preprocessor (which people often with 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'll go badly. A Quick Note on that:everything are this tutorial has been normalized to 4-space tabs, even the code that was auto-generated W ITH 2-space tabs. If you are want to use two or three spaces, or actual tabs (which are 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 be 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 this express installation routine created a file called Package.json in your 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 we 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 we dependencies and we ' re rea

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.