LoginSignup
0
0

More than 3 years have passed since last update.

Node.js + Express + PhpStormでWordpressの投稿をWP REST APIを使って表示する

Posted at

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;

エンドポイントを登録して、

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.

今日はここまで。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0