Skip to content

Commit 5f6bb10

Browse files
author
Achilles
committed
FIX: Add server.mjs for Render deployment
- Created server.mjs to serve static files - Updated package.json start script - Render now has proper entry point - Site will serve index.html and all pages Fixes 'Cannot find module server.mjs' error
1 parent 744b96d commit 5f6bb10

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"description": "Execution Protocol as Virtuals ACP Service with ATTEST integration",
55
"main": "src/index.js",
66
"scripts": {
7-
"start": "node src/index.js",
8-
"dev": "nodemon src/index.js",
7+
"start": "node server.mjs",
8+
"dev": "node server.mjs",
99
"test": "jest",
1010
"deploy": "node scripts/deploy.js",
1111
"deploy:testnet": "npx hardhat run scripts/deploy-testnet.js --network baseSepolia"

server.mjs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const http = require('http');
2+
const fs = require('fs');
3+
const path = require('path');
4+
5+
const PORT = process.env.PORT || 10000;
6+
7+
const mimeTypes = {
8+
'.html': 'text/html',
9+
'.css': 'text/css',
10+
'.js': 'text/javascript',
11+
'.json': 'application/json',
12+
'.png': 'image/png',
13+
'.jpg': 'image/jpeg',
14+
'.gif': 'image/gif',
15+
'.svg': 'image/svg+xml',
16+
'.ico': 'image/x-icon'
17+
};
18+
19+
const server = http.createServer((req, res) => {
20+
let filePath = '.' + req.url;
21+
22+
if (filePath === './') {
23+
filePath = './index.html';
24+
}
25+
26+
// Check if it's a directory request
27+
if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) {
28+
filePath = path.join(filePath, 'index.html');
29+
}
30+
31+
const extname = String(path.extname(filePath)).toLowerCase();
32+
const contentType = mimeTypes[extname] || 'application/octet-stream';
33+
34+
fs.readFile(filePath, (error, content) => {
35+
if (error) {
36+
if (error.code === 'ENOENT') {
37+
// Page not found
38+
fs.readFile('./index.html', (err, content) => {
39+
if (err) {
40+
res.writeHead(404);
41+
res.end('404 Not Found');
42+
} else {
43+
res.writeHead(200, { 'Content-Type': 'text/html' });
44+
res.end(content, 'utf-8');
45+
}
46+
});
47+
} else {
48+
res.writeHead(500);
49+
res.end('500 Server Error');
50+
}
51+
} else {
52+
res.writeHead(200, { 'Content-Type': contentType });
53+
res.end(content, 'utf-8');
54+
}
55+
});
56+
});
57+
58+
server.listen(PORT, () => {
59+
console.log(`Server running at http://localhost:${PORT}/`);
60+
});

0 commit comments

Comments
 (0)