Skip to content

Instantly share code, notes, and snippets.

@Shelob9 Shelob9/1.sh
Last active Oct 31, 2018

Embed
What would you like to do?
mkdir my-app && cd my-app && npm init -y && npm i --save express
app.get("/posts/:postName", async (req, res) => {
const cwd = process.cwd();
const {postName} = req.params;
const path = cwd + '/content/wp-json' + '/posts/' + postName + '.json';
const exists = fs.existsSync(path);
if (exists) {
const post = require(path);
res.json(post.data);
} else {
try{
const site = await require('wpapi')
.discover('https://calderaforms.com');
try{
const post = await site.posts().slug(postName);
if( post.length ){
await fs.writeFileSync(path,JSON.stringify(post[0]));
return res.json(post[0]);
}
res.status(404);
res.json(notFound);
}catch (e) {
res.status(500);
return res.json(e);
}
}catch (e) {
res.status(500);
return res.json(e);
}
}
});
npm i --save react react-dom react-oembed-container
app.get( "/", async(req, res) => {
const server = require( 'react-dom/server');
const React = require( 'react');
const { renderToString } = server;
const cwd = process.cwd();
const postName = 'hello-world';
const path = cwd + '/content/wp-json' + '/posts/' + postName + '.json';
const exists = fs.existsSync(path);
if (exists) {
const post = require(path).data;
return res.send(renderToString( React.createElement('article',{
id: `post-${post.id}`
},[
React.createElement('h2', {}, post.title.rendered),
React.createElement('div', {dangerouslySetInnerHTML: {__html: post.content.rendered}}, ),
]
)));
}
});
const express = require( 'express' );
const app = express();
app.get("/", (req, res) => {
res.send(`Hi Roy!`);
});
const server = app.listen(process.env.PORT, () => {
console.log("Started at http://localhost:%d\n", server.address().port);
});
res.status(418); //Set status code to indicate to client that server is a teapot
return res.json({url: 'https://i.kym-cdn.com/photos/images/original/000/626/354/cea.jpg' }); //Return this object as JSON, with the right headers and stuff
app.get("/posts/:postName", (req, res) => {
const cwd = process.cwd();
const {postName} = req.params;
return res.json({postName});//returns the second part of the URL
});
app.get("/posts/:postName", (req, res) => {
const cwd = process.cwd();
const {postName} = req.params;
const path = cwd + '/content/wp-json/posts/' + postName + '.json';
const post = require(path);
res.json(post.data);
});
const notFound = {
code: "rest_post_invalid_id",
message: "Invalid post ID.",
data: {
status: 404
}
};
app.get("/posts/:postName", (req, res) => {
const cwd = process.cwd();
const {postName} = req.params;
const path = cwd + '/content/wp-json/posts/' + postName + '.json';
const exists = fs.existsSync(path);
if (exists) {
const post = require(path);
res.json(post.data);
} else {
res.status(404);
res.json(notFound);
}
});
npm install --save wpapi
async () => {
const site = await require('wpapi')
.discover('https://calderaforms.com');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.