I am running a website build with next.js using command npm run build. And that build folder is being served by a simple app.js file and run by command node app.js as my server only supports nodejs scripts. Below is the app.js file. Actually, preview for the website is not loading however all the links are there on the html component rendered. I can see opengraph detecting it but other sites are having hard time getting the url, there are no errors in console the build is success.
What would be the reason of this issue?
const http = require('http');
const fs = require('fs');
const path = require('path');
const url = require('url');
const server = http.createServer((req, res) => {
// Step 1: Check params and extract URL
const parsedUrl = url.parse(req.url, true);
let filePath = parsedUrl.pathname;
// Step 2: Check if sitemap
if (filePath === '/sitemap.xml') {
serveStaticFile(path.join(__dirname, 'out', 'sitemap.xml'), res);
return;
} else if (filePath === '/robots.txt') {
serveStaticFile(path.join(__dirname, 'out', 'robots.txt'), res);
return;
}
// Step 3: Serve HTML files or static files directly
if (filePath === '/') {
filePath = '/index.html';
} else {
// Check if the file exists with .html extension
const htmlFilePath = path.join(__dirname, 'out', filePath + '.html');
if (fs.existsSync(htmlFilePath)) {
filePath = filePath + '.html';
}
}
// Serve HTML files or static files directly
serveStaticFile(path.join(__dirname, 'out', filePath), res);
});
// Function to serve static files directly
function serveStaticFile(filePath, res) {
const decodedFilePath = decodeURIComponent(filePath);
fs.readFile(decodedFilePath, (err, data) => {
if (err) {
console.error(`Error reading static file: ${err}`);
// If the file is not found, respond with 404 Not Found
res.writeHead(404, { 'Content-Type': 'text/html' });
fs.readFile(path.join(__dirname, 'out', '404.html'), (err, data) => {
if (err) {
console.error(`Error reading 404 page: ${err}`);
res.end('<h1>404 Not Found</h1>');
} else {
res.end(data);
}
});
} else {
// If the file is found, respond with the file content
res.writeHead(200);
res.end(data);
}
});
}
server.listen();
console.log('website is running')
Tried enabling cache, now using cloudflare. But unable to load preview in most of the social platforms.