From c2e77cdc2a4b11f560b580d4616237efc3be1e72 Mon Sep 17 00:00:00 2001 From: devvsakib Date: Sat, 27 Jul 2024 19:38:38 +0600 Subject: [PATCH] api added for posts --- {posts => public/posts}/index.json | 0 .../pull_request_tutorial_for_beginners.md | 0 src/api/index.js | 23 +++++++++++++++++++ vercel.json | 17 +++++++------- 4 files changed, 31 insertions(+), 9 deletions(-) rename {posts => public/posts}/index.json (100%) rename {posts => public/posts}/pull_request_tutorial_for_beginners.md (100%) create mode 100644 src/api/index.js diff --git a/posts/index.json b/public/posts/index.json similarity index 100% rename from posts/index.json rename to public/posts/index.json diff --git a/posts/pull_request_tutorial_for_beginners.md b/public/posts/pull_request_tutorial_for_beginners.md similarity index 100% rename from posts/pull_request_tutorial_for_beginners.md rename to public/posts/pull_request_tutorial_for_beginners.md diff --git a/src/api/index.js b/src/api/index.js new file mode 100644 index 0000000..4950405 --- /dev/null +++ b/src/api/index.js @@ -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}`); +}); diff --git a/vercel.json b/vercel.json index bb22fe6..e2b4f0d 100644 --- a/vercel.json +++ b/vercel.json @@ -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" } ] } \ No newline at end of file