LoginSignup
9
3

More than 5 years have passed since last update.

nodeで静的ページを動かす最短メモ

Posted at

ちょっとnodeを試してみたいときに使用。

新しいフォルダにnpmの設定ファイルを作る

mkdir new_project
cd new_project
npm init

expressをインストール

expressは簡単なアプリが作れるライブラリ。

npm install express --save

表示するhtmlファイルを用意する

mkdir htdocs
touch htdocs/index.html
touch index.js
index.js
var express = require('express');
var app = express();

app.use(express.static('htdocs'));

var port = 3000;
app.listen(port,function(){
    console.log("Expressサーバーがポート%dで起動しました。モード:%s",port,app.settings.env)
});
index.html
<html>
<head>
    <title>test</title>
</head>
<body>
<h1>テスト</h1>
</body>
</html>

実行

node index.js

下記にブラウザで接続

http://localhost:3000/


使う3つのコマンド

npm init
npm install
npm run

npm installコマンド

個別にインストール

npm install パッケージ名

dependencies内に入ったライブラリはnpm installでインストール

npm install
package.json
{
  "dependencies": { "jquery": "^2.1.3" }
}

参考

フロントエンド開発の3ステップ(npmことはじめ)
http://qiita.com/hashrock/items/15f4a4961183cfbb2658

9
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
3