Nodejs : Expressを使用した簡単なサーバの作成方法



イントロ
それで、我々は我々の機械に乗っています.
私たちも学びました.
また、知っている.
今、我々はどのように使用して簡単なサーバーを作成する方法を学びたいexpress .

簡単なスクリプトを書く
  • 端末を開く
  • ファイル名index.js :
  • touch index.js
    
  • このJavaScriptコードを追加します.
  • // import express (after npm install express)
    const express = require('express');
    
    // create new express app and save it as "app"
    const app = express();
    
    // server configuration
    const PORT = 8080;
    
    // create a route for the app
    app.get('/', (req, res) => {
      res.send('Hello World');
    });
    
    // make the server listen to requests
    app.listen(PORT, () => {
      console.log(`Server running at: http://localhost:${PORT}/`);
    });
    
    注意:この単純なサーバには、1つだけのルートがあります(/ ). ルーティングについてもっと知りたい場合はread the docs for Routing .

    端末から実行する
  • 実行する
  • node index.js
    
  • 結果:
  • Server running at: http://localhost:8080/
    
    今、あなたはリンクをクリックし、作成されたサーバーに到達することができます.

    更なる読書
  • express
  • Routing
  • Response Methods

  • 質問
  • お使いですかexpress またはkoa or sails ? なぜそれを使うのですか.