I'm trying to get the HTML string of two EJS files I have in my project, one is 'index.ejs' and the other is 'page2.ejs'.
I'm rendering these two in my server.js file and they are all working fine.
But the problem is when I want to fetch them to get their HTML using axios, it returns weird data.
My server code to fetch 'index.ejs' to get the HTML:
const express = require('express');
const app = express();
const axios = require('axios');
app.listen(5500)
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
res.render('index');
});
app.get('/page2', (req, res) => {
res.render('page2');
});
axios('http://localhost:5500')
.then(response => console.log(response.data))
.catch(err => console.log(err))
The response I get back:
[nodemon] starting `node server.js`
[
'node_modules',
'package-lock.json',
'package.json',
'public',
'server.js',
'views'
]
And when I try to fetch 'page2.ejs' to get the HTML:
axios('http://localhost:5500/page2')
.then(response => console.log(response.data))
.catch(err => console.log(err))
The response I get back:
[nodemon] starting `node server.js`
Error: Request failed with status code 404
at createError (C:\Users\user\OneDrive\Desktop\Folders\Programming\Web Development\Web-Development-Projects\Test\node_modules\axios\lib\core\createError.js:16:15)
at settle (C:\Users\user\OneDrive\Desktop\Folders\Programming\Web Development\Web-Development-Projects\Test\node_modules\axios\lib\core\settle.js:17:12)
at IncomingMessage.handleStreamEnd (C:\Users\user\OneDrive\Desktop\Folders\Programming\Web Development\Web-Development-Projects\Test\node_modules\axios\lib\adapters\http.js:312:11)
Why am I getting wrong data on the first? and an error on the second?