Brief introduction to using NodeJS + Express development server

With the development of Nodejs, it has been well known to many people, and NodeJs has become a must-have for front-end developers. This article will not introduce too much more of Nodejs. If you can visit NodeJS official website

This article uses NodeJS + Express to develop a server program, Express is a flexible node.js web application framework for maintaining minimal size Provide a set of powerful features for web and mobile applications. See: Official website

First you need to install the Nodejs environment Next,

1. Install Express

NPM Install Express -gnpm install express-generator -g
   
2. Initialization item

   
item name we specified as ApiServer, can be seen from the project name We are analog servers API

Here we will provide a user details interface and output JSON data.

On the last position of the terminal

使用 NodeJS+Express 开发服务端的简单介绍

INSTALL Dependencies: $ CD APISERVER && NPM Install / / Tell us to enter the project root directory to perform NPM Install installation dependencies Run the app: $ debug = apiserver: * npm start // tell usStart server
  Performed as follows:  
1. CD APIServer // Enter the project root directory

2.NPM Install // Installation and dependence

We will see an ApiServer directory I will use Sublime text to open


/ BIN: To start the application (server)

使用 NodeJS+Express 开发服务端的简单介绍 / public: Store Static Resource Directory

/ Routes: Routing to determine how the application responds to a specific endpoint Machine request, contains a URI (or path) and a specific HTTP request method (GET, POST, etc.). Each route can have one or more handler functions, which are executed when routing matches.
/ views: Template file where the file format is .jade


Catalog App.JS program main file This is the entry of the server started


Two start servers


NPM START // Start server

  The terminal will output Node ./bin/www
accessed in the browser http: // localhost: 3000 /

使用 NodeJS+Express 开发服务端的简单介绍
Three basics


Open App.js Here, the main code

VARExpress = Require (‘expression’); var path = Require (‘path’); Var Favicon = Require (‘serve-favicon’); var logger = required (‘Morgan’); var cookieparser = Require (‘cookie-parser ‘); var bodyparser = Require (‘ body-parser ‘); var app = expression (); /// ======= Routing information (interface address) Start storage in ./routes directory ==== ======= // var Routes = Require (‘./ Routes / Index’); // Home Page interface var users = required (‘./ Routes / Users’); // User Interface App.USE ‘/’, routes); // Register Routes in App.use (‘/ users’, users); // Register in the app USERS interface / / / ======= Routing information (interface) Address introduction =========== /// / / ======= Template start ===================== /// View Engine setup app.set (‘ Views’, path.join (__ DIRNAME, ‘Views’); app.set (‘ view engine ‘,’ jade ‘); /// ======= Template end ========= == // 使用 NodeJS+Express 开发服务端的简单介绍

When we accesses http: // localhost: 3000 / call in your browser

We can see INDEX.JS.The definition of this interface:

VAR Express = Require (‘Express’); var Router = Express.Router (); // Defines a GET Request Path as the root directory / * / Router.get (‘/’, function (req, res, next) {res.render (‘index’, {title: ‘express’});}); module.exports = router;
  Defines the basic format of a route to:  

app.method (path, handler)

Where:

APP is an example of Express.
 Method is an HTTP request method.   PATH is the path on the server. 
Handler is a function that is executed when routing match.

The above definition represents

to respond to the GET request on the root route (/) (the home page of the application):
Does it understand?
  If we want to implement a user information interface?  
It is very simple to create a USER.JS file in the routes directory. The content is as follows:


Define a USER model


Function user () {this.name; this.city; this.aGe;} module.exports = user;


switched to users.js file


in the file Top add

VAR URL = Require (‘url’);

and continues to add as follows:


Router.get (‘/ getuserinfo’, function (req, res, next) {var user = new user (); var params = url.parse (Req.url, True) .query; if (params.id == ‘1’) {user.name = “light”; user.age = “1”; user.city = “Beijing”;} else {user.name = ” “User.age =” 1 “; user.city =” Hangzhou “;} var response = {status: 1, data: user}; res. extend (Json.Stringify (Response));});

Explanation focus:
  Get URL parameters Depending on the URL module to use Require ('url')  
VAR params = url.parse (Req.url, true) .query;

使用 NodeJS+Express 开发服务端的简单介绍


Since the users.js routing information is already registered in app.js
 can directly access the server to re-START server   call mode 
http: // localhost: 3000 / user / getuserinfo? Id = 1
or

http: // localhost: 3000 / users / getuserinfo? ID = 2

  Are you noticed that we have access to users / getuserinfo? Id = 1 instead of root  
reason is that we are registered in app.js as app. Use (‘/ users’, user);

We can use this way to develop module features such as you have another module for MSG

We are registered as: App .USE (‘/ MSGS’, MSGS);

Call mode is http: // localhost: 3000 / msgs / getusermsgs? id = 1
Here we write dead back Data does not query the database
   Of course, NodeJs has the ability to access MySQL but is not the introduction of this article. You can refer to this article: https://www.jb51.net/Article/110616. HTM 
The above is all the content of this article, I hope to help everyone, I hope everyone will support TUMI clouds.

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment