Node.jsマイクロサービスの簡単な例

1882 ワード

ダウンロードするとjs
公式サイト:https://nodejs.org/en/
ダウンロード:node-v 8.11.3-x64.msi
次に、プロンプトに従ってインストールします.
二インストールが成功したかどうかをテストする
Node.js微服务简单例子_第1张图片
三編纂jsマイクロサービス
1コード
var http = require('http');
var url = require("url");
var path = require('path');

//   server
var server = http.createServer(function(req, res) {
  //        
  var pathname = url.parse(req.url).pathname;
  res.writeHead(200, { 'Content-Type' : 'application/json; charset=utf-8' });
  //   http://localhost:8060/,    {"index":"      "}
  if (pathname === '/') {
    res.end(JSON.stringify({ "index" : "      " }));
  }
  //   http://localhost:8060/health,    {"status":"UP"}
  else if (pathname === '/health.json') {
    res.end(JSON.stringify({ "status" : "UP" }));
  }
  //       404
  else {
    res.end("404");
  }
});
//     ,     
server.listen(8060, function() {
  console.log('listening on localhost:8060');
});

2運転
Node.js微服务简单例子_第2张图片
3テスト
アクセスhttp://localhost:8060/health.json、結果は以下の通り
{"status":"UP"}
アクセスhttp://localhost:8060/、結果は以下の通り
{「index」:「トップページへようこそ」}
アクセスhttp://localhost:8060/k、結果は以下の通り
404