phpのビルトインウェブサーバーからnode.jsのサーバにfetchでリクエスト
注意:これは自分が暇な時間にただだらだらと書いたコードを乗せるだけの投稿です。 技術的に勉強した部分や参考になる部分はありません。
- index.html
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> </style> </head> <body> <script> fetch('http://127.0.0.1:3000') .then(res=>res.json()) .then(json=>console.log(json)) </script> </body> </html>
- index.js
const http=require('http') const server=http.createServer() server.on('request',(req,res)=>{ res.writeHead(200,{ 'Access-Control-Allow-Origin':'*', 'Access-Control-Allow-Headers':'*', 'Content-Type':'application/json', }) res.write(JSON.stringify([1,2,3,4,5])) res.end() }) server.listen(3000,'127.0.0.1') console.log('server listenin...')
- ビルトインウェブサーバ起動
$ php -S 127.0.0.1:8000
- node.jsのサーバ起動
$ node index.js