日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Nodejs in Visual Studio Code 03.学习Express

發(fā)布時間:2025/3/13 编程问答 9 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Nodejs in Visual Studio Code 03.学习Express 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.開始

下載源碼:https://github.com/sayar/NodeMVA

Express組件:npm install express -g(全局安裝)

?

2.ExpressRest

打開目錄08_ExpressREST

app.js

var express = require('express'); var app = express();
//捕獲GET方法,并處理返回一個Json,比C#寫Json簡單多了啊 app.get('/', function (req, res) {res.json({ message: 'hooray! welcome to our api!' }); });
//偵聽8080端口 app.listen(process.env.PORT || 8080);

現(xiàn)在項(xiàng)目都用html/js了,寫個node幾行代碼搞定json restful,還帶web服務(wù)器,感覺神清氣爽啊!

打開CMD讓項(xiàng)目運(yùn)行起來

$ cd 08_EXPRESSREST $ node app

?

3.AdvancedRESTAPI

打開目錄12_AdvancedRESTAPI

app.js:初始化Express環(huán)境注冊路由

routes/api.js :rest api的具體實(shí)現(xiàn)代碼

app.js中,注冊路由代碼十分簡單

//初始化一個api.js實(shí)例 var api = require('./routes/api');//初始化一個express.js實(shí)例 var app = express();//將指定url路由處理程序指向api.js文件 app.use('/api', api);

routes/api.js中對每個api后的url按GET\PUT\POST\DELETE分別處理

Resource

GET

PUT

POST

DELETE

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

List all the 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(dogs); });/* PUT replace all dogs */ router.put('/dogs/', function(req, res, next) {console.log(req.body);dogs = req.body;res.json({"STATUS": "200 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": "200 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": "200 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": "200 OK"});}}return res.json({"STATUS": "404 NOT FOUND"}); });module.exports = router;

  

轉(zhuǎn)載于:https://www.cnblogs.com/mengkzhaoyun/p/5355796.html

與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的Nodejs in Visual Studio Code 03.学习Express的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。