Currently have this piece of code it gets me the data i need but idk how to post/showcase the data on the website page and not the console file? i tried post and everything i could think of
const express = require('express');
const app = express();
app.listen(3000, () =>console.log('success'));
app.use(express.static('public'));
var axios = require('axios');
const { response } = require('express');
var config = {
// method -> 'get', 'post', 'delete', 'put'
method: 'get',
// API endpoint -> 'https://api.eventyay.com/v1/event-locations';
// API endpoint with query parameters -> 'https://api.eventyay.com/v1/event-locations?sort=name'
url: 'https://api.eventyay.com/v1/event-locations',
// sindle header -> header: { 'key': 'value'}
// multiple header -> header: { 'key': 'value', 'key': 'value'}
headers: { }
};
// If API response is success then would be executed, else catch block would be executed.
axios(config).then((response) => {
// data = response.data
// To parse data to json -> JSON.parse(response.data)
// To parse data to string -> JSON.stringify(response.data)
console.log(JSON.stringify(response.data))
return response.data;
})
.catch((error) => {
console.log(error)
});
Try this code, it listens to /my-api-path and returns the result.
const express = require('express')
const app = express()
const port = 3000
var axios = require('axios');
app.use(express.static('public'));
app.get('/my-api-path', (req, res) => {
var config = {
// method -> 'get', 'post', 'delete', 'put'
method: 'get',
// API endpoint -> 'https://api.eventyay.com/v1/event-locations';
// API endpoint with query parameters -> 'https://api.eventyay.com/v1/event-locations?sort=name'
url: 'https://api.eventyay.com/v1/event-locations',
// sindle header -> header: { 'key': 'value'}
// multiple header -> header: { 'key': 'value', 'key': 'value'}
headers: { }
};
// If API response is success then would be executed, else catch block would be executed.
axios(config).then((response) => {
// data = response.data
// To parse data to json -> JSON.parse(response.data)
// To parse data to string -> JSON.stringify(response.data)
console.log(JSON.stringify(response.data))
res.send(JSON.stringify(response.data));
})
.catch((error) => {
console.log(error)
res.send("Error "+error.toString());
});
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})