How to Create Simple JSON-Source Endpoint using Express
1 min readOct 23, 2019
Sometime you might need to create a simple endpoint for tutorials and mini projects,
Advantages of this method are :
- No need to setup complex database config
- Useful for simple example project (for tutorials, mini project, etc)
- Easy to create and config your own JSON file
Here’s step-by-step to create your own service endpoint using JSON file, with express.
- Install Node Js
- Create file named server.js as follows :
//server.js
const data = require('./json/user.json')
const express = require('express')
const app = express()
const port = 3000app.get('/', (req, res) => res.send('Hello World!'))app.get('/user', function (req, res) {
res.json(data);
})app.listen(port, () => console.log(`Example app listening on port ${port}!`))
3. Create json file, you can use the help of json generator (https://www.json-generator.com/) as follows :
//user.json
[
{
"user_id": "5dafc00fcf290b2c87adc27c",
"balance": "$2,452.36",
"age": 36,
"name": "Taylor Mooney"
},
{
"user_id": "5dafc00fb80d4ee1233a2eb9",
"balance": "$1,835.88",
"age": 27,
"name": "Barrera Dillard"
},
...
4. Run your server, make sure PORT 3000 is not used. Run it using command ``node server.js``
5. Result :
You can user this as following to your simple mini project.