Skip to content

Commit bbaa511

Browse files
committed
API with HTTP Auth 📦
1 parent bfdfcee commit bbaa511

File tree

6 files changed

+103
-3
lines changed

6 files changed

+103
-3
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ yarn start
4242
http://localhost:3005/
4343
```
4444

45-
- Post via API (SOON)
45+
- Post via API - it Require Basic HTTP Auth - Update password in `config.js` file
46+
47+
```sh
48+
http://localhost:3005/api?title=This%20is%20Example%20Post%20title&description=This%20is%20Example%20Post%20Meta%20Description%20-%20post%20via%20HTTP%20Client%20via%20API.&postcontent=This%20is%20Example%20Post%20Meta%20Description%20-%20post%20via%20HTTP%20Client%20via%20API.&tag=Test
49+
```
50+
51+
- if you want to update HTTP auth username find this line `users: { 'admin':sitedata.password },` in `index.js` - Default username `admin` and password `123456789`
4652

4753
## Modification
4854

config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"url_data": "example-blog",
33
"storage_path": "./posts",
4-
"format": "md"
4+
"format": "md",
5+
"password": "123456789"
56
}

index.js

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {
1111
check,
1212
validationResult
1313
} = require('express-validator');
14+
const basicAuth = require('express-basic-auth');
1415

1516
const app = express();
1617
const port = 3005;
@@ -64,7 +65,7 @@ app.post('/post', csrfProtection, [
6465
}),
6566
check('description', 'description length should be 100 to 140 characters Good for SEO')
6667
.isLength({
67-
min: 100,
68+
min: 60,
6869
max: 155
6970
}),
7071
check('postcontent', 'Fill Some Post Content').not().isEmpty().trim().escape(),
@@ -120,6 +121,74 @@ app.post('/post', csrfProtection, [
120121
}
121122
});
122123

124+
app.get('/api', basicAuth({
125+
users: { 'admin':sitedata.password },
126+
challenge: true,
127+
unauthorizedResponse: 'not authorized'
128+
}), [
129+
check('title', 'title length should be 50 to 60 characters Good for SEO')
130+
.isLength({
131+
min: 10,
132+
max: 65
133+
}),
134+
check('description', 'description length should be 100 to 140 characters Good for SEO')
135+
.isLength({
136+
min: 60,
137+
max: 155
138+
}),
139+
check('postcontent', 'Fill Some Post Content').not().isEmpty().trim().escape(),
140+
check('tag', 'Enter Atleast one Tag for Post').not().isEmpty().trim().escape(),
141+
], function(req, res) {
142+
143+
res.header('X-Frame-Options', 'DENY');
144+
res.header('X-XSS-Protection', '1; mode=block');
145+
res.header('X-Content-Type-Options', 'nosniff');
146+
res.header('Strict-Transport-Security', 'max-age=63072000');
147+
148+
const blog_title = req.query.title
149+
const random_id = Math.floor(1000 + Math.random() * 9000)
150+
const basename = sitedata.url_data + "-" + random_id
151+
152+
const errors = validationResult(req);
153+
154+
if (!errors.isEmpty()) {
155+
res.status(400).json(errors);
156+
} else {
157+
158+
const seo_url = slugify(blog_title, {
159+
replacement: '-',
160+
remove: /[*+~.()'"!:@]/g,
161+
lower: true,
162+
strict: false
163+
});
164+
165+
var title = blog_title;
166+
var description = req.query.description;
167+
var date = formattedDate;
168+
var tag = req.query.tag;
169+
var postcontent = req.query.postcontent;
170+
let content = [{
171+
title: title || "Example Post title",
172+
description: description || "Example Post description",
173+
date: date,
174+
tag: tag || "Hello World",
175+
postcontent: postcontent || "Example Post Content",
176+
slug: decodeURIComponent(seo_url)
177+
}];
178+
let template = fs.readFileSync("./template.md").toString()
179+
content.forEach(post_data => {
180+
let output = render(template, post_data)
181+
const clean_url = basename;
182+
fs.writeFileSync(`${sitedata.storage_path}/${clean_url}.${sitedata.format}`, output)
183+
console.log(post_data);
184+
})
185+
res.status(200).json({
186+
sucess: 1,
187+
message: 'Post Created'
188+
});
189+
}
190+
});
191+
123192
app.use('/', function(req, res) {
124193
res.status(404).json({
125194
error: 1,

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"cookie-parser": "^1.4.5",
2929
"csurf": "^1.11.0",
3030
"express": "^4.17.1",
31+
"express-basic-auth": "^1.2.0",
3132
"express-validator": "^6.13.0",
3233
"hbs": "^4.1.2",
3334
"mustache": "^4.2.0",

posts/example-blog-3937.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: "This is Example Post title"
3+
description: "This is Example Post Meta Description - post via HTTP Client via API."
4+
date: 2021-11-10
5+
tags:
6+
- "Test"
7+
---
8+
9+
This is Example Post Meta Description - post via HTTP Client via API.

yarn.lock

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ balanced-match@^1.0.0:
6464
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
6565
integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==
6666

67+
basic-auth@^2.0.1:
68+
version "2.0.1"
69+
resolved "https://registry.yarnpkg.com/basic-auth/-/basic-auth-2.0.1.tgz#b998279bf47ce38344b4f3cf916d4679bbf51e3a"
70+
integrity sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==
71+
dependencies:
72+
safe-buffer "5.1.2"
73+
6774
binary-extensions@^2.0.0:
6875
version "2.2.0"
6976
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.2.0.tgz#75f502eeaf9ffde42fc98829645be4ea76bd9e2d"
@@ -350,6 +357,13 @@ etag@~1.8.1:
350357
resolved "https://registry.yarnpkg.com/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887"
351358
integrity sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=
352359

360+
express-basic-auth@^1.2.0:
361+
version "1.2.0"
362+
resolved "https://registry.yarnpkg.com/express-basic-auth/-/express-basic-auth-1.2.0.tgz#a1d40b07721376ba916e73571a60969211224808"
363+
integrity sha512-iJ0h1Gk6fZRrFmO7tP9nIbxwNgCUJASfNj5fb0Hy15lGtbqqsxpt7609+wq+0XlByZjXmC/rslWQtnuSTVRIcg==
364+
dependencies:
365+
basic-auth "^2.0.1"
366+
353367
express-validator@^6.13.0:
354368
version "6.13.0"
355369
resolved "https://registry.yarnpkg.com/express-validator/-/express-validator-6.13.0.tgz#270d9e8718386b04b1880b5bb7b2c1c2a91a412b"

0 commit comments

Comments
 (0)