Hello world example
以下に埋め込まれているのは、基本的に作成できる最も簡単なExpressアプリです。 It is a single file app — not what you’d get if you use the Express generator, which creates the scaffolding for a full app with numerous JavaScript files, Jade templates, and sub-directories for various purposes.
const express = require('express');const app = express();const port = 3000;
app.get('/', (req, res) => { res.send('Hello World!');});
app.listen(port, () => { console.log(`Example app listening on port ${port}`);});このアプリはサーバーを起動し、接続のためにポート3000をリッスンします。 アプリはルート URL (/) または route にリクエスト
を返します。 他のすべてのパスについては、404 Not Foundで応答します。
ローカルで実行中
最初に myapp という名前のディレクトリを作成し、変更して npm init を実行します。 Then, install express as a dependency, as per the installation guide.
myapp ディレクトリで、app.jsという名前のファイルを作成し、上の例からコードをコピーします。
The req (request) and res (response) are the exact same objects that Node provides, so you can
invoke req.pipe(), req.on('data', callback), and anything else you would do without Express
involved.
次のコマンドでアプリを実行します。
$ node app.js次に、ブラウザーに http://localhost:3000/ をロードして出力を確認します。