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