Node.jsを体験するための小さな例

        新しいフォルダーを作成します --> 新しいフォルダーの第 1 レベルのディレクトリに新しいserve.js ファイルを作成します -->serve.js に次のコードを記述します。

const http = require('http')

const hostname = '127.0.0.1'
const port = 3000

const server = http.createServer((req, res) => {
    res.statusCode = 200
    res.setHeader('Content-Type', 'text/plain')
    res.end('Hello World\n')
})

server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`)
})

        次に、cmd コマンド ウィンドウに新しく作成したフォルダー ディレクトリを入力し、nodeserve.js コマンドを入力して上記のコードを実行します。

         次に、ブラウザで取得したリンク ( http://127.0.0.1:3000/ ) にアクセスすると、次のような効果が得られます。

 

おすすめ

転載: blog.csdn.net/m0_59778008/article/details/131553087