-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
60 lines (51 loc) · 1.41 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const express = require("express");
const cors = require("cors");
require("dotenv").config();
var bodyParser = require("body-parser");
const jwt = require("jsonwebtoken");
const axios = require("axios");
const app = express();
const port = process.env.PORT || 4000;
const URL = process.env.URL;
const ADMIN_KEY = process.env.ADMIN_KEY;
app.use(cors());
app.use(bodyParser.json({ limit: "50mb", extended: true }));
app.use(
bodyParser.urlencoded({
limit: "50mb",
extended: true,
parameterLimit: 50000,
})
);
app.use(bodyParser.text({ limit: "200mb" }));
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.post("/post", (req, res) => {
const key = ADMIN_KEY;
const [id, secret] = key.split(":");
const token = jwt.sign({}, Buffer.from(secret, "hex"), {
keyid: id,
algorithm: "HS256",
expiresIn: "5m",
audience: `/admin/`,
});
const url = `${URL}/ghost/api/admin/posts/?source=html`;
const headers = { Authorization: `Ghost ${token}` };
const post = {
title: req.body.title,
html: req.body.html,
status: "published",
excert: req.body.excert,
tags: [{ name: "user" }],
};
const payload = { posts: [post] };
axios
.post(url, payload, { headers })
.then((response) => res.send("post success"))
.catch((error) => res.send(error));
});
app.listen(port, () => {
console.log(port, ": port");
console.log(`listening on port ${port}`);
});