As a newcomer to this, my goal is to initiate an external API call after entering a term in a search form and then migrating to a new page displaying the outcomes. Here's what I have accomplished thus far.
const express = require('express');
const request = require('request');
const path = require('path');
const app = express();
const PORT = 3000;
const url =
'https://www.example.com/api/search_term?query=';
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.listen(PORT, () => {
console.log(`Example app listening on port ${PORT}!`);
});
app.get('/search', (req, res) => {
res.render('search', { title: 'Hey', message: 'Hello there!' });
});
app.post('/results', (req, res) => {
const term = req.body.term;
request(`${url}term`, (error, response, body) => {
if (!error && response.statusCode === 200) {
console.log(body);
}
});
});
In my PUG file, all that exists presently is a basic search form. My confusion arose when I considered how to redirect from a get
request due to the fact that I'm calling an external API.
The plan is to launch the application, establish the search form as the main page, input a term for searching, and then transition to a second page with the search results.
I'm under the assumption that I must re-direct to another get
route to retrieve the outcomes from the initial get
route containing the search form.