Skip to content

Commit

Permalink
api added for posts
Browse files Browse the repository at this point in the history
  • Loading branch information
devvsakib committed Jul 27, 2024
1 parent d252757 commit c2e77cd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
File renamed without changes.
23 changes: 23 additions & 0 deletions src/api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
const port = process.env.PORT || 3000;

app.use(express.static(path.join(__dirname, 'public')));

// Route to get the JSON data
app.get('/posts/index.json', (req, res) => {
fs.readFile(path.join(__dirname, 'public', 'posts', 'index.json'), 'utf8', (err, data) => {
if (err) {
res.status(500).send('Server Error');
return;
}
res.header("Content-Type", "application/json");
res.send(data);
});
});

app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
17 changes: 8 additions & 9 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
{
"rewrites": [
{
"source": "/posts/(.*)",
"destination": "/posts/$1"
},
{
"source": "/(.*)",
"destination": "/"
}
"version": 2,
"builds": [
{ "src": "api/index.js", "use": "@vercel/node" },
{ "src": "public/**/*", "use": "@vercel/static" }
],
"routes": [
{ "src": "/api/(.*)", "dest": "/api/index.js" },
{ "src": "/(.*)", "dest": "/public/$1" }
]
}

0 comments on commit c2e77cd

Please sign in to comment.