JS
Node JS
How To Create Basic API In Node JS Express?
let's see article of how to create api in node js. We will look at example of how to create api in node js express. you can understand a concept of node js create api example. if you have question about create simple api using node js then i will give simple example with solution.
- TAGS
- Node JS
- 4.5/5.0
- Last updated 08 September, 2022
- By Admin
Here, i will give you very simple example how to create simple api in node js with express. we will create "users" api and return users.json file data. so let's follow bellow step to create simple example
Step 1: Create Node App
run bellow command and create node app
mkdir my-app cd my-app npm init
Install Express using bellow command:
npm install express --save
Step 2: Create server.js file
Here, we will create simple users api with GET method.
server.js
const express = require('express') const fs = require('fs') const app = express() app.get('/', (req, res) => { res.end('Hello World!, Example from codewale.com'); }); app.get("/users", (req, res) => { fs.readFile(__dirname + '/' + 'users.json', 'utf8', (err, data) => { res.end(data); }); }); app.listen(3000, () => { console.log(`app listening at //localhost:3000`) });
Now let's create users.json file as bellow:
users.json
[ { "id": 1, "name": "hardik", "email": "hardik@gmail.com" }, { "id": 2, "name": "Vimal", "email": "vimal@gmail.com" }, { "id": 3, "name": "Harshad", "email": "harshad@gmail.com" } ]
now you can simply run by following command:
node server.js
Output:
I hope it can help you...
Categories
PHP
(5)
Codeigniter
(5)
Laravel
(3)
JS
(1)
Angular JS
(5)
Node JS
(5)
Vue JS
(4)
React JS
(6)
jQuery
(5)
Javascript
(3)
jQuery UI
(6)
Wordpress
(1)
Python
(5)
Database
(1)
MySql
(5)
Mongo DB
(1)
DBMS
(1)
SQL
(5)
HTML
(1)
HTML 5
(5)
CSS
(5)
Bootstrap
(5)
Ajax
(5)
JAVA
(1)
JSON
(5)
Ubuntu
(5)
Server
(5)
Git
(3)
Google
(1)
Google Map
(7)
Google API
(1)
Elastic Search
(1)
Apache
(1)
Installation
(1)