Node.js + Express + PhpStormでWordpressの投稿をWP REST APIを使って表示する
PhpStormでNode.js Express Appを新規作成
File -> NewProject -> Node.js Express App
この時のオプションは以下の通りです。
Options
- View Engine: EJS
- Stylesheet Engine: Plain CSS
terminalで WP APIをインストール
npm install wpapi --save
routes/index.jsを編集
var express = require('express');
var router = express.Router();
var WPAPI = require( 'wpapi' );
var wp = new WPAPI({ endpoint: 'https://localhost/my-local-test/wp-json/' });
/* GET home page. */
router.get('/', function(req, res, next) {
wp.posts().then((posts) => {
console.log(posts);
res.render('index', { title: 'Express' });
});
});
module.exports = router;
npm install wpapi --save
var express = require('express');
var router = express.Router();
var WPAPI = require( 'wpapi' );
var wp = new WPAPI({ endpoint: 'https://localhost/my-local-test/wp-json/' });
/* GET home page. */
router.get('/', function(req, res, next) {
wp.posts().then((posts) => {
console.log(posts);
res.render('index', { title: 'Express' });
});
});
module.exports = router;
エンドポイントを登録して、
var wp = new WPAPI({ endpoint: 'https://localhost/my-local-test/wp-json/' });
getリクエストの処理を書きます。
router.get('/', function(req, res, next) {
wp.posts().then((posts) => {
console.log(posts);
res.render('index', { title: 'Express' });
});
});
ローカル環境では取れましたが、やりたいサイトではエラーがー出てしまってます。
(node:25928) UnhandledPromiseRejectionWarning: TypeError: Cannot create property '_paging' on string ''
at returnBody (C:\Users\Koji\PhpstormProjects\wordpress-api-test\node_modules\wpapi\lib\http-transport.js:244:16)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:25928) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:25928) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
今日はここまで。
Author And Source
この問題について(Node.js + Express + PhpStormでWordpressの投稿をWP REST APIを使って表示する), 我々は、より多くの情報をここで見つけました https://qiita.com/donkey-maru/items/690ad57d6f83c0577c2e著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .