Nodejs in Visual Studio Code 03. Learn Express

Source: Internet
Author: User

1. Start

Download Source: Https://github.com/sayar/NodeMVA

Express components: NPM install EXPRESS-G (global installation)

2.ExpressRest

Open Directory 08_expressrest

App.js

var express = require (' Express '); var app = Express ();
Capturing the Get method and processing it returns a JSON that is much simpler than C # writing JSON app.get ('/', function (req, res) { Res.json ({message: ' hooray! Welcome to our Api! '});
Listen for 8080 port app.listen (Process.env.PORT | | 8080);

Now the project is used Html/js, write a few lines of code to fix JSON restful, also with Web server, feel refreshed!

Open cmd to get the project up and running

$ CD 08_expressrest$ node app

3.AdvancedRESTAPI

Open Directory 12_advancedrestapi

App.js: Initialize Express Environment Registration route

Specific implementation code for the Routes/api.js:rest API

App.js, registering the routing code is simple

Initialize a api.js instance of var API = require ('./routes/api ');//Initialize a Express.js instance var app = Express ();// Point the specified URL route handler to the Api.js file App.use ('/api ', API);

The URLs for each API in Routes/api.js are processed separately by Get\put\post\delete

Resource

GET

PUT

POST

DELETE

Collection URI, such as http://api.example.com/v1/dogs/

List all T He dogs

Replace all the dogs with a new collection of dogs.

Create a new dog in the collection.

Delete the entire dog collection.

Element URI, such as HTTP://API.EXAMPLE.COM/V1/DOG/1

Get A Specific dog.

Replace a dog in the collection with another dog.

not used.

Delete the dog from the collection.

var express = require (' Express '); var router = Express.    Router (); var dogs = [{"dog_id": "0", "dog_name": "Ginger"}, {"dog_id": "1", "Dog_name": "Ruby"}, { "dog_id": "2", "Dog_name": "Buddy"}];/* GET All Dogs */router.get ('/dogs/', function (req, res, next) {Res.json (do GS);});  * PUT Replace All dogs */router.put ('/dogs/', function (req, res, next) {Console.log (req.body);  Dogs = Req.body; Res.json ({"STATUS": "$ OK"}); * POST Create a new dog */router.post ('/dogs/', function (req, res, next) {Dogs.push (req.body) Res.json ({"STATUS": "200 OK "});  * Delete Delete the entire dog collection */router.delete ('/dogs/', function (req, res, next) {dogs = []; Res.json ({"STATUS": "$ OK"});  * GET a specific dog */router.get ('/dogs/:id ', function (req, res, next) {var i = 0;  var dog = null;      for (i = 0; I! = Dogs.length; i++) {if (dogs[i].dog_id = = req.params.id) {dog = Dogs[i];    Break }} dog!== null? Res.json (dog): Res.json ({"STATUS":" 404 Not FOUND "})});/* PUT replace a specific dog with another dog */router.put ('/dogs/:id ', function (req, res, next) {  var i = 0;  var dog = null;      for (i = 0; I! = Dogs.length; i++) {if (dogs[i].dog_id = = req.params.id) {dog = Dogs[i];    Break  }} if (dog!== null) {dog.dog_name = req.body[' Dog_name '] Res.json ({"STATUS": "OK"});  } else {Res.json ({"STATUS": "404 Not FOUND"});  }});/* DELETE a specific dog from the collection */router.delete ('/dogs/:id ', function (req, res, next) {var i = 0;      for (i = 0; I! = Dogs.length; i++) {if (dogs[i].dog_id = = req.params.id) {Dogs.splice (I, 1);    Return Res.json ({"STATUS": "$ OK"}); }} return Res.json ({"STATUS": "404 Not FOUND"}); Module.exports = router;

  

Nodejs in Visual Studio Code 03. Learn Express

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.