Nodejs : Expressを使用した簡単なサーバの作成方法
4011 ワード
イントロ
それで、我々は我々の機械に乗っています.
私たちも学びました.
また、知っている.
今、我々はどのように使用して簡単なサーバーを作成する方法を学びたいexpress .
簡単なスクリプトを書く
index.js
:touch index.js
// 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
またはkoa or sails ? なぜそれを使うのですか.Reference
この問題について(Nodejs : Expressを使用した簡単なサーバの作成方法), 我々は、より多くの情報をここで見つけました https://dev.to/miku86/nodejs-how-to-create-a-simple-server-using-express-1n9dテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol