--- lang: ja-jp breaks: true --- # Node.js で Hello World Ubuntu 20.04 2021-04-30 ## コンソールにHello World ```shell= $ vim main.js ``` ```javascript= console.log("こんにちは 世界"); ``` ```shell= $ node main.js こんにちは 世界 ``` ## WebブラウザにHello World > Node.js v10.24.1 Documentation Usage & Example > https://nodejs.org/docs/latest-v10.x/api/synopsis.html ```javascript= const http = require('http'); const hostname = '127.0.0.1'; const port = 4000; const server = http.createServer( (req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain; charset=utf-8'); res.end('今日は世界!!\n'); } ); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); } ); ``` :::info :bulb: ※外部からアクセスするには、'127.0.0.1'をネットワークIFのIPアドレスに変更する必要がある。 ※Visual Studio CodeからSSHで接続して実行したら、ポートフォワーディングでWindows側からアクセス可能。 ::: ```shell= $ node web.js Server running at http://127.0.0.1:4000/ ``` ###### tags: `Node.js` `Hello World` `Webアプリ` `Ubuntu 20.04`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up