在节点js中创建rest api

Representational state transfer (REST) is an architectural design pattern for APIs. APIs that follow this pattern are called REST APIs or RESTful APIs. REST sets certain standards between computer systems on the web that make it easier for systems to communicate with each other.

代表性状态转移(REST)是API的体系结构设计模式。 遵循此模式的API称为REST API或RESTful API。 REST在Web上的计算机系统之间设置了某些标准,这些标准使系统之间更易于通信。

In this article, We learning that hoe to create REST Apis in NodeJs and use it. In fact, we will set up a complete project through an npm module.

在本文中,我们学习了在NodeJs中创建并使用REST Apis的方法 。 实际上,我们将通过npm模块设置一个完整的项目。

有一些基本步骤 (There are some basic steps)

  1. Setup environment

    设定环境
  2. Initialize the project

    初始化项目
  3. Change directory

    变更目录
  4. Install dependencies

    安装依赖项
  5. Start project

    开始项目

设置环境:- (Setup environment:-)

Firstly, We check there are all modules are installed in our local machine or not. There are some required modules and some optional. Node is required and npm, nvm are optional. I am supposing that node is available in your machine if it’s not then follow. https://blog.teamtreehouse.com/install-node-js-npm-mac

首先,我们检查本地计算机上是否安装了所有模块。 有一些必需的模块和一些可选的模块。 节点是必需的, npmnvm是可选的。 我猜想该节点在您的计算机中是可用的,如果没有,那么请遵循。 https://blog.teamtreehouse.com/install-node-js-npm-mac

You can check the node is available or not in your machine through the following command.

您可以通过以下命令检查node是否在计算机中可用。

$ node -v
Image for post
node version
节点版本

初始化项目或生成项目:- (Initialize the project OR Generate project:-)

You can initialize the project from scratch via npm init command. But there is another way, you can install express-generator the module and create a pre-structured project or boilerplate that will help you to forward your project without any interruptions.

您可以通过npm init命令从头开始初始化项目。 但是还有另一种方法,您可以安装express-generator模块,并创建一个预构建的项目或样板,这将帮助您无中断地转发项目。

$ sudo 
Image for post
install express-generator
安装Express-Generator

After installing express-generator , We will create a node project through the following command.

安装express-generator ,我们将通过以下命令创建一个节点项目。

$ express 
Image for post
create a project
创建一个项目

更改目录: (Change directory:-)

After creating first-node-ap , We enter the directory through the following command.

创建first-node-ap ,我们通过以下命令进入目录。

$ cd first-node-app
Image for post
change directory
改变目录
Image for post
project structure
项目结构

安装依赖项:- (Install dependencies:-)

After Entering the directory, We install dependencies through the following command.

进入目录后,我们通过以下命令安装依赖项。

$ npm install
Image for post
install dependencies
安装依赖

After installing dependencies there will be one extra folder, which is node_modules . You can see the given image.

安装依赖项后,将有一个额外的文件夹,即node_modules 。 您可以看到给定的图像。

Image for post
structure
结构体

开始项目:- (Start Project:-)

After completing everything we start the project through the following command.

完成所有操作后,我们通过以下命令启动项目。

$ npm start
Image for post
start project
开始项目

After starting the project, You will have to search http//:localhost:3000 you will find the following page.

启动项目后,您将必须搜索http//:localhost:3000您将找到以下页面。

Image for post
first route
第一条路线
Image for post
second route
第二条路线

In this project, two routes are already available. Now you can add your own routes and forward this project according to you.

在此项目中,两条路线已经可用。 现在,您可以添加自己的路线,并根据您的要求转发此项目。

That’s it for this time! I hope you enjoyed this post. As always, I welcome questions, notes, comments and requests for posts on topics you’d like to read. See you next time! Happy Coding !!!!!!

这次就是这样! 希望您喜欢这篇文章。 与往常一样,我欢迎您提出关于您要阅读的主题的问题,注释,评论和帖子请求。 下次见! 快乐编码!!!!!!

翻译自: https://medium.com/@rajputankit22/create-rest-api-in-node-js-f131e72de331

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值