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 operation
res.end ('will send all the dishes to you!');
} );
app.post ('/dishes', (req,res,next) => { //post operation
res.end ('will add the dish:' + req.body.name + 'with details:' + req.body.description );
});
app.put ('/dishes', (req,res,next) => { //put opration
res.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