December 2015 node. JS Combat (i) build multiplayer blogs with Express+mongodb

Source: Internet
Author: User

node is a server-side scripting language based on the V8 engine.

Basic Preparation

node. JS:

Express: This article is 3.21.2 version, the latest version of 4.13.3,EXPRESS4 and EXPRESS3 is still a big difference, you can go to the official website to view wiki:https://github.com/strongloop/express

Mongodb:

First, use Express to build a Site 1 Quick start to install Express

Express is the most popular web development framework on node that allows you to quickly develop a Web application. Enter the command in global mode:

$ NPM install-g [email protected]

2 Create a new project

We set up the project in the D-disk directory, cmd into D-Disk, enter:

$ EXPRESS-E Blog

Then enter:

$ cd Blog & NPM Install

Install required modules (This command is often used when adding modules to a site)

3 Starting the site

Enter in the global mode:

$ node App

At this point the command line shows Express server listening on Port 3000, and the browser accesses localhost:3000 to see the following:

4 Express Engineering directory structure

App.js: Entry File

Package.json: Engineering information and dependency modules

Node_modules: Store the modules installed in the Package.json

Public: Store Image/css/js and other documents

Routes: Storing Routing files

Views: Store An attempt to file, or say a template file

5 Routing control

Express encapsulates a variety of HTTP requests, mainly using get and post, i.e. App.get () and App.post ().

The first parameter of App.get () and App.post () is the requested path, and the second parameter is the callback function that handles the request, respectively req and res, representing the request information and the response information:

App.get ('/',function(req, res) {    res.render (' index ', {title: ' Express 123 '});

There are several ways to process requests:

Req.query: Handling get requests, getting GET request parameters

Req.body: Handles a GET or POST request in/:xxx form to get the request parameters.

Req.params: Processing the POST request to obtain the POST request body.

Req.param (): Handles get and post requests, but finds priority from high to low to Req.params->req.body->req.query

6 Ejs Template Engine

The template engine is a tool that combines page templates and data to be displayed to generate HTML pages.

The Ejs template engine is used in this article

      1. by App.set (' View engine ', ' Ejs '); set up using the Ejs template engine.
      2. by App.set (' views ', Path.join (__dirname, ' views ')), set the location where the template files are stored.
      3. Res.render (' index ', {title: ' Express 123 '), renders the template and returns the resulting page directly to the client. The first parameter, index, is the name of the template, omitting the. ejs extension, and the second parameter {title: ' Express 123 '} is a data object
7 Ejs Label System

Only three types of labels

<% code%> Display JS codes
<%= code%> display Raw HTML content
<%-code%> displays content that has been replaced with HTML special characters

Note: When code is normal, <%= code%> and <%-code%> the same result.

When code is

8 Ejs's include

Index.ejs

<%-Inclue a%>
hello,world!
<%-Inclue b%>

A.ejs

This is A.ejs

B.ejs

This is B.ejs

The final Index.ejs will be displayed as:

This is A.ejs

hello,world!

This is B.ejs

Second, build a multi-person blog

December 2015 node. JS Combat (i) build multiplayer blogs with Express+mongodb

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.