Node.jsでwebサーバーを立てる
Node.jsでWebサーバーを立てる方法。
httpモジュールを使うとWebサーバーを立てることができます。
Node.jsのプログラムの前にVagrantfileを変更します。
192.168.33.10で仮想環境にアクセスできるようにしておきます。
Vagrantfile
# Create a private network, which allows host-only access to the machine
# using a specific IP.
- # config.vm.network "private_network", ip: "192.168.33.10"
+ config.vm.network "private_network", ip: "192.168.33.10"
変更したらvagrant upで立て直します。
次にNode.jsのプログラムを作ります。
index.js
'use struct'
var http = require('http');
const server = http.createServer(function (req, res) {
res.writeHead(200, {'COntent-Type': 'text/plan'});
res.end('Hello World');
});
const port = 8080;
server.listen(port, () => {
console.log('listening on ' + port);
})
コンソール
node index.js
ブラウザで192.168.33.10:8080を開いて
Hello Worldが表示されたら成功
ディスカッション
コメント一覧
まだ、コメントがありません