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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

express rest_Express / Node中用于REST API的邮递员工具

發布時間:2025/3/11 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 express rest_Express / Node中用于REST API的邮递员工具 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

express rest

When dealing with routes (like in express), we may use any of the REST verbs and at times, the browser is limited to facilitate testing the routes/REST API.

在處理路由時(如快速表達),我們可以使用任何REST動詞,有時瀏覽器會受到限制,以方便測試路由/ REST API。

REST API的郵遞員工具 (Postman tool for REST API)

POSTMAN is a tool which helps us easily work with REST API. It has it's desktop and web version (chrome extension).

POSTMAN是可幫助我們輕松使用REST API的工具。 它具有桌面版和網絡版(chrome擴展名)。

Before we see how it works, let's remind ourselves about the various REST API operations according to courser.org node.js tutorial video.

在了解其工作原理之前,讓我們根據courser.org node.js教程視頻提醒自己各種REST API操作。

Take Note! You should have Node js installed in your computer.

做記錄! 您應該在計算機中安裝Node js。

With Node.js already up and running, let's get started.

在Node.js已經啟動并運行的情況下,讓我們開始吧。

Download POSTMAN follow the installation procedure to install or download on the web as chrome extension...

按照安裝程序下載POSTMAN ,以chrome擴展程序的形式在網絡上安裝或下載...

I personally used the web version, but using the PC software is the same.

我個人使用過網絡版本,但是使用PC軟件是相同的。

Wait for a while as it downloads.

等待下載。

NB: Internet required!

注意:需要互聯網!

In this article we’re going to create an express route or REST API and use POSTMAN. Open a text editor and type the following code and save it with the file name app.js:

在本文中,我們將創建一個快速路由或REST API并使用POSTMAN 。 打開文本編輯器,輸入以下代碼,并將其保存為文件名app.js:

const express = require ('express'); const http = require ('http'); const morgan = require('morgan'); const bodyParser = require ('body-parser');const hostname = 'localhost'; const port = 8080;const app = express(); app.use (morgan('dev')); app.use (bodyParser.json ());app.all('/dishes', (req,res,next) => {res.statusCode = 200;res.setHeader ('Content-Type', 'text/plain');next(); } );app.get ('/dishes', (req,res,next) => { // get operationres.end ('will send all the dishes to you!'); } ); app.post ('/dishes', (req,res,next) => { //post operationres.end ('will add the dish:' + req.body.name + 'with details:' + req.body.description ); });app.put ('/dishes', (req,res,next) => { //put oprationres.statusCode = 403;res.end ('put operation not supported on dishes'); });app.delete ('/dishes', (req,res,next) => {res.end ('deleting all dishes!'); } );app.get ('/dishes/:dishId', (req,res,next) => {res.end ('will send will send details of the dish:' + req.params.dishId + 'to you' ); } ); app.post ('/dishes/:dishId', (req,res,next) => {res.statusCode = 403;res.end ('post operation not supported on /dishes/' + req.params.dishId); });app.put ('/dishes/:dishId', (req,res,next) => {res.write('updating the dish:' + req.params.dishId)res.end ('will update the dish:' + req.body.name + 'with details' + req.body.description ); });app.delete ('/dishes/:dishId', (req,res,next) => {res.end ('deleting dish:' + req.params.dishId); } );app.use (express.static (__dirname + '/tools')); app.use ( (req,res,next) => { res.statusCode = 200; res.setHeader ( 'content-type', 'text/html' ); res.end ('<html><body><h1>This is an express Server</h1></body></html>') }); const server = http.createServer (app); server.listen (port,hostname, () =>{console.log (`server running at http://${hostname}:${port}`)} );

The file should be saved in your default node.js project directory.

該文件應保存在默認的node.js項目目錄中。

Initiate the JavaScript file at the console by typing node app.js

通過鍵入節點app.js在控制臺上啟動JavaScript文件

Take Note!: Your command line directory should be same with node js module/project directory.

注意!:您的命令行目錄應與node js module / project目錄相同。

Open your browser and navigate to http://localhost:8080

打開瀏覽器并導航到http:// localhost:8080

Now, let's open post man and see how it works.

現在,讓我們打開post man,看看它是如何工作的。

It's user interface is very simple to understand.

它的用戶界面很容易理解。

All the REST properties can be seen below, left just to input URL, select operation, and send.

所有REST屬性都可以在下面看到,僅用于輸入URL,選擇操作和發送。

Thanks for coding with me. Your comments are most welcome.

感謝您與我一起編碼。 非常歡迎您發表評論。

翻譯自: https://www.includehelp.com/node-js/postman-tool-for-rest-api-in-express-node.aspx

express rest

總結

以上是生活随笔為你收集整理的express rest_Express / Node中用于REST API的邮递员工具的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。