Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8829

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

$
0
0

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を編集

varexpress=require('express');varrouter=express.Router();varWPAPI=require('wpapi');varwp=newWPAPI({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;

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

varwp=newWPAPI({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.

今日はここまで。


Viewing all articles
Browse latest Browse all 8829

Trending Articles