Node.js+express4+nunjucks+supervisor installation using a small note

Source: Internet
Author: User
Tags install node

We're all familiar with node. JS and you don't have to introduce too much.

The homepage of each project:

node.js:http://nodejs.org

Express:http://expressjs.com

nunjucks:http://mozilla.github.io/nunjucks/

Supervisor:https://github.com/isaacs/node-supervisor

Nunjucks is inspired by Python's Jinja2 template, and if you have a Django template enthusiast using Python, you can try swig.

Replace the default Jade because it prefers this style of not forcing indentation, and Jade itself is hard to write

1 <  class= "{% if Loop.index = = Current_nav_index%}active{% endif%}">

This kind of code is difficult to judge, because the target requires a web Page, not a web App, and so does not consider a scenario like the Angularjs class.

Supervisor is used to monitor the JS code, once the code changes can automatically restart the service process, easy to develop.

=================================================

1. Install node. js

RVM-inspired NVM (https://github.com/creationix/nvm/) is strongly recommended under LINUX/MAC, and other platforms are described in the official installation instructions.

2. Install Express

Later versions of Express 4.x recommend the use of the Express-generator component:

NPM install-g Express-generator

After the installation is complete, you can use

Express MyApp

Create a new MyApp project, or add the-C option to specify a preprocessing engine that uses less.

3. Installing Nunjucks

To enter the MyApp directory, first install Nunjucks as a dependency:

NPM Install Nunjucks--save

Because we don't need the jade processing template anymore, we can do it:

NPM Uninstall Jade--save

Unload Jade Dependency.

After the installation is complete, the nunjucks needs to be loaded by modifying the app.js.

When you add the necessary

var nunjucks = require (' nunjucks ');

After that, you need to

var app = Express ();

After adding

Nunjucks.configure (' views ', {    true,    Express:app});

and will

App.set (' View engine ', ' Jade ');

Change into

App.set (' View engine ', ' html ');

That is, you can place the Nunjucks template under the Views folder with the suffix of the HTML.

Also, the original jade template needs to be modified to look like this:

Layout.html

1 <!DOCTYPE HTML>2 <HTML>3 <Head>4   <title>{{title}}</title>5   <Linkrel= "stylesheet"href= "/stylesheets/style.css">6 </Head>7 <Body>8 {% block content%}{% endblock%}9 </Body>Ten </HTML>

Index.html

1 {% extends ' layout.html '%} 2 3 {% block content%} 4   < H1 > {{title}} < H1 > 5   < P > Welcome to {{title}}</p>6 {% Endblock%}

Error.html

1 {% extends ' layout.html '%}2 3 {% block content%}4   <H1>{{Message}}<H1>5   <H2>{{Error.status}}<H2>6   <Pre>{{Error.stack}}</Pre>7{% Endblock%}

4. Installing Supervisor

NPM install-g Supervisor

After the installation is complete, execute

NPM Install

After you've installed all of your express dependencies, you can

Supervisor./bin/www

Run the Express server.

The ps:./bin/www file can be modified by modifying the file

App.set (' Port ', Process.env.PORT | | 3000);

Change the default listening port in 3000.

Node.js+express4+nunjucks+supervisor installation using small notes

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.