NodeJS-1 building a simple node JS application

Source: Internet
Author: User

Here are a few tips on how to build a simple node. js application.

Build node. JS Services from scratch

Build a Node.js+express.js Service

Build a simple node. js + express.js + handlerbars service from scratch

Build node. JS Services from scratch

Excerpt from node for learning

Code is as follows:

$ node var http = require (' http '); > Http.createserver (function(req, res) {   Res.writehead, {' Content-type ': ' Text/plain ' });   Res.end (' Hello world\n ');}). Listen (8124, "127.0.0.1");
    • Start node first.
    • You can define an HTTP server on the command line.

Build a Node.js+express.js Service

1. Installing Nodejs

2. Installing Express.js

NPM Install-g Express

NPM install-g Express-generator

Express Testexpress

CD testexpress

NPM Install

Node app

This allows you to install a jade as a template application.

build a simple node. js + express.js + handlerbars service from scratch

Original video link: http://vimeo.com/91564681

Ignoring the description of Git is summarized below.

0. Create a directory

mkdir Node-express-handlebars

1. NPM Install

CD Node-express-handlebars

NPM Install

/* Accept all the default configurations. */

2. Install Express

NPM Install Express--save

3. Installing Express-handlebars

NPM Install Express-handlebars--save

4. Create a Server.js

varExpress = require (' Express ');varApp =Express ();varExhbs = require (' Express-handlebars ')); App.engine (' Handlebars ', Exhbs ({' defaultlayout ': ' Main '}); App.set (' View engine ', ' handlebars '); App.get (‘/‘,function(req, res) {varLucknumber = Math.Round (Math.random () * 10); Res.render (' Index ', {lucknumber:lucknumber});}); App.use ('/public ', express.static (' Public '));varPort = number (Process.env.PORT | | 5000); App.listen (port);

5. Create a views directory

As shown below

Main.handlebars

<!DOCTYPE HTML><HTML><Head>    <MetaCharSet= "Utf-8" />    <title>Example App</title></Head><Body>{{{body} }}</Body></HTML>

Index.handlebars

This is index.handlebarshellow there, your lucky number is {{Lucknumber}}

6. Run

Nodemon Server.js

NodeJS-1 building a simple node JS application

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.