-
-
Save Shelob9/3d953c8cf56ecd7903eca64a2926f003 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mkdir my-app && cd my-app && npm init -y && npm i --save express |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm i --save react react-dom react-oembed-container |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}}, ), | |
] | |
))); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
res.status(418); //Set status code to indicate to client that server is a teapot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm install --save wpapi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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