Create a node. JS Web site that provides data APIs

Source: Internet
Author: User

Create a Site Directory

First, create a folder to save your site files, and use mkdir to do it.

PS c:\> mkdir MySite

Then, go to this folder for the next steps.

Create Package Description

Create a Package.json file using Notepad or any plain text editor you like, and the file name is a convention that must be the name.

Create a description of the site itself that describes the other packages that depend on it.

{    "name": "Express-api",    "version": "0.0.1",    "dependencies": {         "Express": "2.5.9",        "Ejs": "0.4.2"    }}
Install the required packages

Then, execute the package that the NPM install depends on. NPM will automatically look for the Package.json file in the current directory.

After installation, there is one more folder Node_modules in the directory.

Create a program

Start writing your node. js code using any of your favorite plain text editors, if you have Visual Studio of course better.

Let's get our servers up and running first. Create a code file named Server.js, and write the following to create a Web server.

var express = Require ("Express"); var app = express.createserver (); Console.log ("Web Server listening ..."); App.listen ( 3000);

At the command line, execute the following command, node server, to start our site. You will see that the Web server is already running.

PS c:\mysite>...

However, to visit, you will find that the page can not be found, of course, we are now a page is not AH!

Create a View

As you can see, our site relies on a module called Ejs, which provides view support, and we can create a page using a view similar to ASP.

In the root directory of the site, create a folder called views, Save our view.

Create our Welcome page INDEX.EJS, note the extension, which is the Ejs convention.

< H1 > Data API</H1><p>welcome! </ P >

Modify our code to return to this view when accessing/.

var express = require ("Express"), var app = Express.createserver (), App.set ("View Engine", "Ejs"), App.set ("views", __ DirName + "/views"), App.set ("View Options", {layout:false}), App.get ("/", function (request, response) {    response. Render ("index");}); Console.log ("Web Server listening ..."); App.listen (3000);

A few additional sets are used to illustrate that our view engine uses the Ejs engine, our view files are saved in the Views folder in the current directory, and the layout is not currently used.
The added get indicates that when a GET request is received/accessed, we need to use a view named Index to render the returned result.

Now the visit to http://127.0.0.1:3000 will receive such a response.

Create data

We want to provide the data API, where is the data stored?

We first save a JSON file that contains the user's information, named Data.json.

{    "1": {        "name": "Tom",        "email": "[email protected]"    },    "2 ": {         " name ":" Red ",        " email ":" [email protected] "    }}

Then, we want to get the user's information through the user's identity.
Add the following paragraph to our code.

function (Request, Response) {    var id = request.params.id;    Console.log (ID);    Response.json (Data[id]) ;

: ID indicates that this is a parameter, which can be obtained through request.params.id in the handler function.

The newly added first line is used to load our data files Data.json, do not forget the previous./, or node will go to Node_modules to find this module.
Now our code becomes this.

vardata = require ("./data"));varExpress = Require ("Express");varApp =express.createserver (); App.set ("View Engine", "Ejs"); App.set ("Views", __dirname + "/views"); App.set ("View Options", {layout:false}); App.get ("/",function(Request, Response) {Response.render ("Index");}); App.get ("/user/:id",function(Request, response) {varID =request.params.id;    Console.log (ID); Response.json (Data[id]); Console.log ("Web Server listening ..."); App.listen (3000);

Now, entering/USER/1 in the address bar, you will receive a JSON return result, which is the information of user number 1th.

How many lines do you use to implement a simple API service?

Including empty lines is only 20 rows.

Create a node. JS Web site that provides data APIs

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.