Skip to content

Commit 43be07b

Browse files
committed
[ADD] Init app
1 parent bf572e3 commit 43be07b

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

api/.gitkeep

Whitespace-only changes.

api/user.json

Whitespace-only changes.

index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const path = require('path');
2+
const polka = require('polka');
3+
const logger = require('./module/logger');
4+
const serveStatic = require('serve-static');
5+
6+
const PORT = parseInt(process.env.PORT, 10) || 3000;
7+
const publicDir = path.join(__dirname, 'public');
8+
9+
const serve = serveStatic(publicDir, { index: ['index.html'] });
10+
const app = polka();
11+
12+
function handleLog(req, res, next) {
13+
logger.info(`Request: ${req.method} ${req.url}`);
14+
next();
15+
}
16+
17+
function handleHeaders(req, res, next) {
18+
res.setHeader('X-Content-Type-Options', 'nosniff');
19+
res.setHeader('X-Frame-Options', 'DENY');
20+
res.setHeader('X-XSS-Protection', '1; mode=block');
21+
next();
22+
}
23+
24+
app.use(handleLog);
25+
app.use(handleHeaders);
26+
app.use(serve);
27+
28+
app.get('/', (req, res) => {
29+
res.sendFile(publicDir + '/index.html');
30+
});
31+
32+
app.get('*', (req, res) => {
33+
res.setHeader('Content-Type', 'application/json');
34+
res.end(JSON.stringify());
35+
});
36+
37+
app.listen(PORT, () => {
38+
console.log(`> Running on localhost:${PORT}`);
39+
});

module/logger.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const pino = require('pino');
2+
3+
const logger = pino({
4+
transport: {
5+
target: 'pino-pretty', //npm install pino-pretty
6+
options: {
7+
colorize: true,
8+
},
9+
},
10+
});
11+
12+
module.exports = logger;

package.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"private": false,
3+
"name": "json-serve-api",
4+
"version": "0.1.0",
5+
"license": "MIT",
6+
"bugs": {
7+
"url": "https://github.com/arashyeganeh/json-serve-api/issues"
8+
},
9+
"author": {
10+
"name": "Arash Yeganeh",
11+
"url": "https://github.com/arashyeganeh"
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "https://github.com/arashyeganeh/json-serve-api.git"
16+
},
17+
"scripts": {
18+
"dev": "nodemon index.js",
19+
"serve": "set PORT=80 && node index.js",
20+
"prettier": "prettier . --write"
21+
},
22+
"devDependencies": {
23+
"nodemon": "^3.1.9",
24+
"prettier": "3.4.2"
25+
},
26+
"dependencies": {
27+
"pino": "^9.6.0",
28+
"pino-pretty": "^13.0.0",
29+
"polka": "^0.5.2",
30+
"serve-static": "^1.16.2"
31+
}
32+
}

0 commit comments

Comments
 (0)