Hi I am trying to build a really simple "API-gateway" to demonstrate a small scale microservice project. I am using Nodejs and Express and I wanted to write a really simple public facing api-gateway server to route requests to my different microservices. For example lets say I have microservice A B and C. I wanted requests to localhost:3000/api/A to go to microservice A and return the result and then all other requests to localhost:3000/api/B go to microservice B and return the result ect. I wanted to write this instead of using NGINX, could someone help me understand how I can achieve this? (Most of my other "microservices" are also nodejs/express apis)
Could I get a quick simple example in code? I would love to see a GET request to google and then the client be able to get the GET request. (use of other libraries or modules would be cool too! :D)
You can run B on port 3001, C on 3002.
Then dispach all request by A on port 3000.
You can use Http client like axios in A to request B or C.
Example
A.js
const express = require('express')
const axios = require('axios')
const app = express()
app.get('/api/B', (req, res) => {
axios.get('http://localhost:3001/dosomething', {
params: {...req.query, name: 'Device A'}, //Fetch datas from params
}).then(result => {
res.send(result)
})
})
app.get('/api/C', (_, res) => {
axios.get('http://localhost:3002/dosomething').then(result => {
res.send(result)
})
})
app.listen(3000, () => console.log('On 3000'))
B.js
const express = require('express')
const app = express()
app.get('/dosomething', (req, res) => {
const data = req.query
//Do something with data fetched here you know...
res.send('No worry, I will do something for ' + data.name)
})
app.listen(3001, () => console.log('On 3001'))
If all micro-services are deployed on the same machine(different machines just need a bit more syncing to know ports/ips but that should not be an issue), you just use a common file to store ops/ports and then just redirect calls from route ip:3000/api/A to ipA:portA
Related
I made a Express.js system where the files in the /routes folder are acting as a classic route (but with one file per route)
Example: /routes/get/user.js will be accessible with http://localhost:8080/user (the /get is to separate methods, it can be /post, /put...)
Here's my entire index.js file: https://pastebin.com/ALtSeHXc
But actually, my problem is that I can't pass params into the url like https://localhost:8080/user/random_id_here.
With this system, I think the best idea is to find a way to pass params on separated files too, but I don't know how can it be done...
Here's an example of one of my separated file:
module.exports = class NameAPI {
constructor(client) {
this.client = client
}
async run(req, res) {
// Code here
}
}
Maybe you'll have a better system, or a solution for this. Thanks.
You can get the optional params from the module object you already have so each module specifies its own params. This example below shows just adding new params on after the module name, but you could extend this feature to be richer if you needed to.
In a simple implementation, in your loader, you can change this:
posts.forEach((post) => {
const module = new (require(`./routes/post/${post}`))(this);
this.api.post(`/${post}`, async (req, res) => await module.run(req, res))
})
to this:
posts.forEach((post) => {
const module = new (require(`./routes/post/${post}`))(this);
const urlParams = module.params || "";
this.api.post(`/${post}${urlParams}`, async (req, res) => module.run(req, res))
});
So, if a given route wanted the extra URL param /:id added to it, then it would just define the .urlParams property on its exported module object to be `"/:id" and that would be automatically included in the route definition.
P.S. Most of the code in each of the branches of your switch statement in _loadHttpMethode() is identical. With a little factoring into a common function and one or two parameters passed to that function, you can eliminate all the copied code among those different branches of the switch so all each switch does is call one function and pass it a few arguments.
I generally would setup my express to handle this way in that case you want a dynamic insert. This is personal code so do make the adjustments necessary or observe the behavior! :)
WEBAPP.get('/room/:name', (req, res) => {
// Check if URL ends with / (in my case I don't want that)
if (req.url.endsWith('/')) return res.redirect('/');
// Check if URL param "name" matches my regex ex. Username1920 or redirect them
if (req.params.name.match(/^[a-zA-Z0-9]{3,24}$/) === null) return res.redirect('/');
// render the room (sending EJS)
res.render('room', {
title: req.params.name.toUpperCase()
});
});
/*
/*This example accepts one param and must follow my regex/rules*/
So if you were handed /room/test12345 your req.params.name would return a value. Notice the colon to define a param, so you could have /:room/:user/:request and it'd return:
req.params.room, req.params.user, req.params.request all defined! :)
How to seperate routes and parse request parameter in express API
You can just put all your different API methods for each model in a seperated folder, and then just parse the API routes in your main file.
Let's say we have one main file called app.js, and you can organize your API routes/endpoints in subfolders.
folder structure:
├── app.js
└── routes
└── api
└── users.js
users.js in the folder routes/api in this case contains all operations for your users endpoint, and you import this in your app.js file.
Based on the route examples defined below, this will let you parse your express API with these endpoints:
GET YOUR_API:PORT/users // fetch all users
GET YOUR_API:PORT/users/:userId // fetch single user by id
app.js
// this is just a demo app.js, please adapt to your needs
const express = require("express");
// express app
const app = express();
app.use(express.json());
// api routes
// endpoint No. 1, this will create the endpoint /users
// and will enable you to use all methods defined in file users.js
app.use("/users", require("./routes/api/users"));
// add more endpoints, example endpoint No. 2
app.use("/ENDPOINT_NAME", require("./routes/api/FILE_NAME"));
// handle everything else you need to handle in your main file
// run server
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));
in subfolder routes/api you add your api route files, like so:
routes/api/users.js
const express = require('express');
const router = express.Router();
// here we only GET all users and user by id,
// but you can add any endpoints required for your API.
// get all users
router.get('/', async (req, res) => {
try {
// do something to get all users
const allUsers = // fetch all users, depends on how you want to do it
return res.status(200).json(allUsers)
} catch (err) {
console.log(err)
return res.status(400).json({ msg: 'Bad request.' })
}
})
// get a specific user by Id from request parameters
router.get('/:userId', async (req, res) => {
try {
// user id from params
const userId = req.params.userId
// do something with this userId, for example look up in DB
return res.status(200).json({userId: `Requested userId is ${userId}`})
)
} catch (err) {
console.log(err)
return res.status(400).json({ msg: 'Bad request.' })
}
})
// add more user endpoints here
// with router.post, router.put, router.delete, whatever you need to do
module.exports = router
I am using axios to call the firebase cloud functions I have created with express/firebase. I realized even without using and without importing the firebase and without initializeApp(firebaseConfig) in my frontend, I was able to call my cloud functions with the axios with the following way:
axios.get('https://us-central1...')
How I create cloud functions:
index.js
module.exports ={
...require('./controllers/foo')
}
foo.js
const functions = require('firebase-functions');
const express = require('express');
const cors = require('cors');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
const app = express();
//Middleware
app.use(express.json());
app.use(cors({ origin: true}));
app.get('/', async (req, res) => {
// Function goes in here
});
app.post('/', async (req, res) => {
// Function goes in here
});
app.put('/:id', async (req, res) => {
// Function goes in here
});
app.delete('/:id', async (req, res) => {
// Function goes in here
});
exports.foo = functions.https.onRequest(app);
Is this a normal behavior or is it due to way of working of express (cors as middleware) or axios somehow? Or do I just have to add auth and firestore rules into my database? Whatever the reason is then what is the meaning of the firebaseConfig information that firebase provides for us?
PS: Just to play around I have tried to add firestore rules. I have added the following rule:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}
}
}
Even though in rules playground I was not able to retrieve anything, from my application I still got the query I wanted and I don't know why.
Yes that is absolutely normal. The HTTP Functions are made so you can integrate you Firebase Project with any (absolutely any) other language or platform by using HTTP requests as the trigger name shows. As you already do, you can even implement express apps behind those requests.
With those you gave full power and responsebility what goes trough them and with that comes also the need for you to know who is calling your HTTP requests. if you want to secure them you can use the link from the comment and check how to make Authorized HTTP Ednpoinds.
If you want to call those just from your App Frontend I would recommend to use Callable Firebse Functions because those will work only from your app and also provide the data of the user who called them in the context of your cloud function triggered.
I have a web app made in node.js and vanilla javascript. I wanna replace "http://localhost:4000/api/word" with "api/word" in the fetch api so that it works when the app's deployed on Heroku. I solved the issue by adding "proxy" : "http://localhost:4000" in package.json file when I used React for other apps but I don't know how to deal with the issue when I'm not using React.
server.js
const express = require("express");
const app = express();
const cors = require("cors");
const fs = require("fs");
const port = process.env.PORT || 4000;
app.use(express.json());
app.use(cors());
app.get("http://localhost:4000/api/word", function (req, res) {
fs.readFile("./wordlist.txt", (err, data) => {
if (err) throw err;
let wordList = data.toString().split("\n");
res.send(wordList);
});
});
main.js
function getWord() {
fetch("/api/word")
.then((res) => res.json())
.then((res) => {
...do something...
})
.catch((err) => console.log(err));
}
I tried the React way but it sends the get request to localhost:5500 which is the client side port.
Since your client and server are listening on different ports, I'm assuming your server isn't serving the client and that it has its own server. If the client doesn't need its own separate server, you can serve it from your express app by putting it in a directory and using express.static. Assuming you put the frontend code in a directory called public next to your server code, that would look like this:
app.use(express.json());
app.use(cors());
app.use(express.static(path.resolve(__dirname, 'public')));
If you do need to have a separate server for the client, there are modules just for this problem. http-proxy is a very popular one. I provided examples of how to use it here that could be easily adapted for any Node server, but there are many more in the docs.
Also just a sidenote, app.get("http://localhost:4000/api/word", function (req, res) should be app.get('/api/word' ...: your routes shouldn't define the scheme, host, and port.
I'm working on an extremely simple CRUD backend using Express and MongoDB. It doesn't even have a frontend and I'm just using Postman to verify that each request is working as expected. Here's what my single-page app looks like so far:
server.js
const express = require('express')
const bodyParser = require('body-parser')
const MongoClient = require('mongodb').MongoClient
let ObjectId = require('mongodb').ObjectId;
const app = express()
const uri = 'mongodb+srv://<USER>:<PW>#<REDACTED>.mongodb.net/test?retryWrites=true'
let db
MongoClient.connect(uri, { useNewUrlParser: true }, (err, client) => {
if (err) return console.log(err)
db = client.db(<COLLECTION_NAME>)
app.listen(3000, () => {
console.log('Listening on port 3000')
})
})
app.put('/todo', (req, res) => {
db.collection('todo').updateOne({_id:
ObjectId(req.body.id)}, {
$set: {item: req.body.value}
}, (err, result) => {
if (err) return console.log(err)
res.send('Todo updated')
})
})
I've already populated my cluster's collection in MongoDB Atlas using a POST request (not shown) that works. Here is what I've been trying in Postman after running the server locally:
The id of the existing Todo is well-defined in the Atlas cluster, but when I log the value of req.body.value in the first line of the callback function of the PUT request in server.js, it's showing the existing value that's in the Todo in that cluster, not what's actually being supplied via Postman. Why is the request body from Postman not being recognized for this request?
This post was helpful to figuring out what was wrong. I needed to import the bodyParser.json() middleware and include it in the PUT request. I then used raw json in the Postman client to successfully send PUT requests.
I'm working on my first real-world Node project using Koa2 and Request for making RESTful API calls to a 3rd party. The Koa service itself is relatively simple, but I'm trying to write an integration test for it using Jest. I've found examples of using Jest with Supertest/Superagent, but I cant find how I'd write the equivalent test using ONLY Jest and Request as the http client. Below is the Jest/Supertest example...
const request = require('supertest');
const app = require('../../src/app')
describe('Test the root path', () => {
test('It should response the GET method', async () => {
const response = await request(app).get('/');
expect(response.statusCode).toBe(200);
});
})
It seems like I should be able to just use Request to do the same thing that supertest/superagent is doing here, but I cant find any example. Thanks for suggestions!
Supertest looks magical because you can pass it your app and somehow that just works.
Under the hood, Supertest justs starts listening and configures the underlying request to use the address as a base url.
I'm using Axios here as an example, I don't use Request but it should be easy enough to adjust.
const axios = require('axios')
const app = require('../../src/app')
const server = app.listen() // random port
const url = `http://127.0.0.1:${server.address().port}`
const request = axios.create({ baseURL: url })
describe('Test the root path', () => {
test('It should response the GET method', async () => {
const response = await request.get('/')
expect(response.statusCode).toBe(200)
});
})