Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8695

npm と node.js のインストールから Hello World まで。

$
0
0

Linux もしくは Windows Subsystem for Linux のターミナルから以下のコマンドを実行し、npm と node.js をインストールする。

sudo sed-i-e's%http://.*.ubuntu.com%http://ftp.jaist.ac.jp/pub/Linux%g' /etc/apt/sources.list
   sudo apt update
   sudo apt upgrade
   sudo apt-get install-y nodejs
   sudo apt install npm

任意のフォルダを作成し、その中で npm init コマンドを実行し package.json を作成する。
expressのパッケージをインストールし、アプリケーションのエントリーポイントとして app.js を作成する。

mkdir hoge
   cd hoge
   npm init
   npm i express --savetouch app.js

package.json の中身は次の通り。npm init の実行時になされる質問で main で指定するエントリーポイントをデフォルトから app.js に変更している。

{"name":"hoge","version":"1.0.0","description":"","main":"app.js","scripts":{"test":"echo \"Error: no test specified\"&& exit 1"},"author":"Matsuoka","license":"MIT","dependencies":{"express":"^4.17.1"}}

app.js の中身は次の通り。

consthttp=require('http');constexpress=require('express');constapp=express();app.use(function(req,res,next){returnres.send('こんにちは世界!');});letserver=http.createServer(app);server.listen('3000');

以下のコマンドで、node から app.js を実行する。

node app.js 

http://localhost:3000/をブラウザで開く。

コメント 2020-07-24 003701.png


Viewing all articles
Browse latest Browse all 8695

Trending Articles