Making a function an express middleware in a route - javascript

So I have a route and I have some ugly code in the route that I'd like to make a middleware. The only problem is that Express's documentation isn't clear and my code just 404s.
How can I accomplish this?
Route:
router.get('/product/:slug', function(req, res) {
//route params
var slug = req.params.slug;
var productResp; //scope up api response to pass to render()
console.log(slug);
//api call
Prismic.api("https://prismic.io/api").then(function(api) {
return api.getByUID('product' , slug);
}).then(function(response) {
app.use(markedHtml)
})
.catch(function(error) {
res.render('404');
})
});
Function:
var markedHtml = function(req, res, next) {
var md_col_1 = response.data["product.markdown-col-one"].value[0].text;
var md_col_1_1 = response.data["product.markdown-col-one"].value[1].text;
var md_col_2 = response.data["product.markdown-col-two"].value[0].text;
var md_col_2_1 = response.data["product.markdown-col-two"].value[1].text;
var md_col_3 = response.data["product.markdown-col-three"].value[0].text;
var md_col_3_1 = response.data["product.markdown-col-three"].value[1].text;
var html_col_1 = marked(md_col_1);
var html_col_1_1 = marked(md_col_1_1);
var html_col_2 = marked(md_col_2);
var html_col_2_1 = marked(md_col_2_1);
var html_col_3 = marked(md_col_3);
var html_col_3_1 = marked(md_col_3_1);
res.render('product-template', {
product: response,
md_one: html_col_1,
md_one_1: html_col_1_1,
md_two: html_col_2,
md_two_1: html_col_2_1,
md_three: html_col_3,
md_three_1: html_col_3_1,
})
next();
}

In your root app.js, you should see all of the middleware your app is using. These middleware are matched sequentially. If they do not include the route argument, they will naturally be applied to all requests. Your routes are among them, and they look something like this: app.use('/', routes);.
Underneath your routes in app.js, declare a new one:
const markedHtml = require('./middleware/markedHtml');
app.use('/product/:slug', markedHtml);
In ./middleware/marketHtml.js, your method will appear like so, without the next call:
const markedHtml = function(req, res, next) {
// Do stuff with req.apiResponse here.
...
res.render('product-template', {
...
});
}
module.exports = markedHtml;
Your original route will look like this:
router.get('/product/:slug', function(req, res, next) {
//route params
var slug = req.params.slug;
var productResp; //scope up api response to pass to render()
console.log(slug);
//api call
Prismic.api("https://prismic.io/api").then(function(api) {
return api.getByUID('product' , slug);
}).then(function(response) {
req.apiResponse = response;
next();
})
.catch(function(error) {
res.render('404');
});
});
Essentially, your original route will receive the client request, do the API calls etc, then (pun!) run into the next(); invocation. That cues express to execute the next applicable middleware, which happens to be markedHtml, from where the view render method will be invoked.
Let me know if anything was unclear or requires additional explanation.
Additional documentation can be found here: http://expressjs.com/en/4x/api.html#app.use

Related

Unit Testing Controllers use Jest, NodeJS

I want to check a case that certain routes are calling the correct controller use Jest specific (mock or spy).
It is case specific for unit testing. Somebody can help me how to check it use jest. I don't need verify kind of
expect (status code or res object) i need to check if controller have been called.
Thanks!
For instance:
// todoController.js
function todoController (req, res) {
res.send('Hello i am todo controller')
}
// index.spec.js
const express = require('express');
const request = require('request-promise');
const todoController = require('./todoController');
jest.mock('./todoController');
const app = express();
app.get('/todo', todoController)
test('If certain routes are calling the correct controller , controller should to have been called times one.', async() => {
await request({url: 'http://127.0.0.1/todo'})
expect(todoController).toHaveBeenCalledTimes(1);
})
Actually if you search, there are many references out there.
In the following, I share a few ways that I know.
One of the big conceptual leaps to testing Express applications with mocked request/response is understanding how to mock a chained
API eg. res.status(200).json({ foo: 'bar' }).
First you can make some kind of interceptor, this is achieved by returning the res instance from each of its methods:
// util/interceptor.js
module.exports = {
mockRequest: () => {
const req = {}
req.body = jest.fn().mockReturnValue(req)
req.params = jest.fn().mockReturnValue(req)
return req
},
mockResponse: () => {
const res = {}
res.send = jest.fn().mockReturnValue(res)
res.status = jest.fn().mockReturnValue(res)
res.json = jest.fn().mockReturnValue(res)
return res
},
// mockNext: () => jest.fn()
}
The Express user-land API is based around middleware. AN middleware that takes a request (usually called req), a response (usually called res ) and a next (call next middleware) as parameters.
And then you have controller like this :
// todoController.js
function todoController (req, res) {
if (!req.params.id) {
return res.status(404).json({ message: 'Not Found' });
}
res.send('Hello i am todo controller')
}
They are consumed by being “mounted” on an Express application (app) instance (in app.js):
// app.js
const express = require('express');
const app = express();
const todoController = require('./todoController');
app.get('/todo', todoController);
Using the mockRequest and mockResponse we’ve defined before, then we’ll asume that res.send() is called with the right payload ({ data }).
So on your test file :
// todo.spec.js
const { mockRequest, mockResponse } = require('util/interceptor')
const controller = require('todoController.js')
describe("Check method \'todoController\' ", () => {
test('should 200 and return correct value', async () => {
let req = mockRequest();
req.params.id = 1;
const res = mockResponse();
await controller.todoController(req, res);
expect(res.send).toHaveBeenCalledTimes(1)
expect(res.send.mock.calls.length).toBe(1);
expect(res.send).toHaveBeenCalledWith('Hello i am todo controller');
});
test('should 404 and return correct value', async () => {
let req = mockRequest();
req.params.id = null;
const res = mockResponse();
await controller.todoController(req, res);
expect(res.status).toHaveBeenCalledWith(404);
expect(res.json).toHaveBeenCalledWith({ message: 'Not Found' });
});
});
This is only 1 approach to testing Express handlers and middleware. The alternative is to fire up the Express server.

Making a get request from within a get request

I'm pretty new to node.js and express and I was wondering if there's a way to define a route that calls upon another route simply to collect data and not to completely reroute.
I've got a route set up as follows:
app.get("/databases/list", function(req, res) {
db.listDatabases().then(names => {
res.send(names);
});
});
Subsequently I'd like to have a different route, say:
app.get('/whatever', function(req, res) {
// here I'd like to make a call to retrieve the information from the first route
// then I'd like to do something with that information, I want to stay in the same route.
}
Is this possible?
Expanding #marcobiedermann answer, In your case simply make a controller and and use the FUNCTION in both the routes. You don't need to fetch anything.
/// --- Controller ----
class SimpleController {
constructor(db){
this.db = db;
}
listDatabase(/*maybe optional callback*/){
return this.db.listDatabases();//or something....
}
whatever(/*maybe optional callback*/){
return this.listDatabase()
.then(process)
}
}
/// --- Routes ----
const sController = new SimpleController(db);
app.get("/databases/list", function(req, res) {
sController.ListDatabase().then(names => {
res.send(names);
});
});
app.get('/whatever', function(req, res) {
sController.whatever()
.then(....)
}
Yes this is possible.
You have to fetch the data from your first endpoint.
fetch('/databases/list')
.then( … )
This requires the /databases/list route to be defined before your /whatever route.
However, I would strongly advice you to NOT do this.
You should abstract your logic into a controller and call this controller in both of your routes:
const fetchController = {
fetchData: () => {
return fetch('path/to/data/to/fetch')
.then( … )
// or database call or wherever you might get the data from
}
}
app.get('/databases/list', (req, res) => fetchController.fetchData());
app.get('/whatever', (req, res) => fetchController.fetchData());
app.get("/databases/list", async function(req, res) {
return await db.listDatabases();
});
app.get('/whatever', async function(req, res) {
const result = await fetch('path/databases/list');
console.log(result)
});
It might help you, But it's not recommended way. You can create method (common somewhere in the controller) and use that where ever you need.

exported function returns 'undefined' when called inside app.get()

I defined a sequelize model in a file named courses.js as such:
const Sequelize = require('sequelize');
var connection = new Sequelize( ... );
module.exports = Courses = connection.define('courses', { ... });
Then required it in a file called coursescontroller.js:
const Courses = require('../models/courses');
function find_records() {
Courses.findAll().then(function(records) {
return records;
});
}
module.exports.find_records = find_records;
Then finally required the function in the express router file and used it as such:
var express = require('express');
var router = express.Router();
var controller = require('../controllers/coursescontroller');
router.get('/', function(req, res, next) {
var records = controller.find_records();
console.log(records); // console shows undefined
res.send(records); // doesn't send anything back when I make a get request
});
Could you please help me get the find_records() function to work properly and return the database records when called inside the get request callback function.

$http is not working, but request is working in mean stack web app

I have a controller(login.controller.js):
var express = require('express');
var router = express.Router();
var request = require('request');
var config = require('config.json');
router.get('/', function (req, res) {
// log vendor out
delete req.session.token;
// move success message into local variable so it only appears once (single read)
var viewData = { success: req.session.success };
delete req.session.success;
res.render('login', viewData);
});
router.post('/', function (req, res) {
// authenticate using api to maintain clean separation between layers
request.post({
url: config.apiUrl + '/vendors/authenticate',
form: req.body,
json: true
}, function (error, response, body) {
if (error) {
return res.render('login', { error: 'An error occurred' });
}
if (!body.token) {
return res.render('login', { error: body, vendorname: req.body.vendorname });
}
// save JWT token in the session to make it available to the angular app
req.session.token = body.token;
// redirect to returnUrl
var returnUrl = req.query.returnUrl && decodeURIComponent(req.query.returnUrl) || '/';
res.redirect(returnUrl);
});
});
module.exports = router;
Where var request is working accessing Remote REST Api. Where as I have one service(vendor.service.js) which has $http not request. But $http is not working:
(function () {
'use strict';
angular
.module('app')
.factory('VendorService', Service);
function Service($http, $q) {
var service = {};
//Store
service.GetCurrent = GetCurrent;//st_details
return service;
function GetCurrent() {
return $http('http://localhost:3000/api/vendors/st_details').then(handleSuccess, handleError);
}
function handleSuccess(res) {
alert('hi3');
alert(res.data);
return res.data;
}
function handleError(res) {
alert('hi2');
alert(JSON.stringify(res));
return $q.reject(res.data);
}
}
})();
Can I use "var request = require('request');" in this vendor.service.js if yes how? Thanks a million in advance!
It seems like you are not using CORS, which you need to use in order to allow apps to access your API via $http.get
Follow the instructions on https://www.npmjs.com/package/cors to add CORS to your express application and it should work.

Calling Express Route internally from inside NodeJS

I have an ExpressJS routing for my API and I want to call it from within NodeJS
var api = require('./routes/api')
app.use('/api', api);
and inside my ./routes/api.js file
var express = require('express');
var router = express.Router();
router.use('/update', require('./update'));
module.exports = router;
so if I want to call /api/update/something/:withParam from my front end its all find, but I need to call this from within another aspect of my NodeJS script without having to redefine the whole function again in 2nd location
I have tried using the HTTP module from inside but I just get a "ECONNREFUSED" error
http.get('/api/update/something/:withParam', function(res) {
console.log("Got response: " + res.statusCode);
res.resume();
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
I understand the idea behind Express is to create routes, but how do I internally call them
The 'usual' or 'correct' way to handle this would be to have the function you want to call broken out by itself, detached from any route definitions. Perhaps in its own module, but not necessarily. Then just call it wherever you need it. Like so:
function updateSomething(thing) {
return myDb.save(thing);
}
// elsewhere:
router.put('/api/update/something/:withParam', function(req, res) {
updateSomething(req.params.withParam)
.then(function() { res.send(200, 'ok'); });
});
// another place:
function someOtherFunction() {
// other code...
updateSomething(...);
// ..
}
This is an easy way to do an internal redirect in Express 4:
The function that magic can do is: app._router.handle()
Testing: We make a request to home "/" and redirect it to otherPath "/other/path"
var app = express()
function otherPath(req, res, next) {
return res.send('ok')
}
function home(req, res, next) {
req.url = '/other/path'
/* Uncomment the next line if you want to change the method */
// req.method = 'POST'
return app._router.handle(req, res, next)
}
app.get('/other/path', otherPath)
app.get('/', home)
I've made a dedicated middleware for this : uest.
Available within req it allows you to req.uest another route (from a given route).
It forwards original cookies to subsequent requests, and keeps req.session in sync across requests, for ex:
app.post('/login', async (req, res, next) => {
const {username, password} = req.body
const {body: session} = await req.uest({
method: 'POST',
url: '/api/sessions',
body: {username, password}
}).catch(next)
console.log(`Welcome back ${session.user.firstname}!`
res.redirect('/profile')
})
It supports Promise, await and error-first callback.
See the README for more details
Separate your app and server files with the app being imported into the server file.
In the place you want to call your app internally, you can import you app as well as 'request' from 'supertest'. Then you can write
request(app).post('/someroute').send({
id: 'ecf8d501-5abe-46a9-984e-e081ac925def',
etc....
});`
This is another way.
const app = require('express')()
const axios = require('axios')
const log = console.log
const PORT = 3000
const URL = 'http://localhost:' + PORT
const apiPath = (path) => URL + path
app.get('/a', (req, res) => {
res.json('yoy')
})
app.get('/b', async (req, res) => {
let a = await axios.get(apiPath('/a'))
res.json(a.data)
})
app.listen(PORT)

Categories