Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/milan-960/nodejs-swagger-api

Simple REST API on Nodejs server build using swagger-ui and ejs.
https://github.com/milan-960/nodejs-swagger-api

ejs nodejs reactjs rest-api swagger swagger-ui vercel

Last synced: about 2 months ago
JSON representation

Simple REST API on Nodejs server build using swagger-ui and ejs.

Awesome Lists containing this project

README

        

## Follow the steps below to create a RESTful server using swager!

## Here is the Tree

```
┣ 📂public
┣ 📂src
┃ ┣ 📂Data
┃ ┃ ┗ 📜data.js
┃ ┣ 📂Routes
┃ ┃ ┗ 📜posts.js
┃ ┣ 📂views
┃ ┃ ┣ 📂pages
┃ ┃ ┃ ┣ 📜index.css
┃ ┃ ┃ ┣ 📜index.ejs
┃ ┃ ┃ ┗ 📜log.jpeg
┃ ┃ ┣ 📂public
┃ ┃ ┃ ┗ 📜index.css
┃ ┃ ┗ 📜.DS_Store
┃ ┣ 📜.DS_Store
┃ ┗ 📜hello.js
┣ 📜.DS_Store
┣ 📜.babelrc
┣ 📜.gitignore
┣ 📜Readme.md
┣ 📜index.js
┣ 📜nodemon.json
┣ 📜now.json
┣ 📜package-lock.json
┣ 📜package.json
┗ 📜vercel.json
```

## Firstly let’s create a RESTful web server:

## Navigate the cursor to the file I will create the project in the terminal and I am creating the package.json file with the following command:

```
npm init --y
```

## Now let’s add the packages that are necessary to create the server:

```
npm install express cors morgan body-parser dotenv
```

## let’s add the babel modules to the project:

```
npm install @babel/core @babel/node @babel/preset-env
```

## let’s create the .babelrc file and add the following code:

```
{
“presets”: [“@babel/preset-env”]
}
```

## And finally, let’s add the following code to scripts field in the package.json file.

```
"start": "nodemon — exec babel-node src/index.js"
```

### If you don’t have nodemon in your PC, I advise installing it globally.

```
sudo npm install -g nodemon
```

## Adding Swagger to the project

Firstly, let’s install two modules that are necessary for documentation and user interface (UI):

```
npm install swagger-jsdoc swagger-ui-express
```

## And I am importing these two modules in the index.js file:

```
import swaggerUI from “swagger-ui-express”;
import swaggerJsDoc from “swagger-jsdoc”;
```

# 🍎 PS: If you have any other ideas about what can be improved please share your thoughts I would really appreciate that!

 
 

 
 

## Nodejs server built and maintained by [Milan Sachani 💜](https://milansachani.dev)