*Webアプリ作成の備忘録*
この記事を見てわかること
- Expressを使ってWebアプリの雛形を作って、SQL Server 2008 R2 にSequelizeで情報を取得しに行く簡単なAPIを実装すること
前提条件
- 環境など前提条件
- Windows7(64bit)
- nodeコマンドが使えること
- SQL Serverがセットアップ済み
おおまかにやること
- ExpressでWebアプリの雛形を作成
- SQL Server 2008 R2の設定
- アプリの中身を作る
1. ExpressでWebアプリの雛形を作成
*nodeコマンドを使えることを前提に話を進めます
-
expressのスケルトンアプリケーションを作成するコマンドのインストール
$ npm install -g express-generator
*これでexpressコマンドが使えるようになっているので、"$ express version"などして確かめると良いです
-
アプリの雛形を作る(*アプリのファイルを置きたいディレクトリに移動しておく)
-
これでWebアプリに最低限必要なセットを用意してくれる
$ express test-app
*"test-app"はアプリ名
・ 以下のような結果が表示されるcreate : test-app create : test-app/package.json create : test-app/app.js create : test-app/public/javascripts create : test-app/public create : test-app/public/stylesheets create : test-app/public/stylesheets/style.css create : test-app/public/images create : test-app/routes create : test-app/routes/index.js create : test-app/routes/users.js create : test-app/views create : test-app/views/index.jade create : test-app/views/layout.jade create : test-app/views/error.jade create : test-app/bin create : test-app/bin/www install dependencies: > cd test-app && npm install run the app: > SET DEBUG=test-app:* & npm start
-
-
必要なmoduleをインストールする(自動的に一通り揃えてくれる)
$ cd test-app // アプリのディレクトリに移動 $ npm install
-
アプリの起動
$ npm start
*ブラウザでlocalhost:3000にアクセスして、"Welcome to Express"という画面を見ることができればOK
2. SQL Server 2008 R2を立てる
*All in Oneをインストールすると、全て入っていて良い
*ここではSQL Server 2008 R2 Express SP1をインストールしている
-
Web Appからの接続先であるDBとテーブルの作成と初期データを入れる
-
DB名: kirin
-
テーブル設計
- テーブル名:Users
- カラム:
カラム名 型 null メモ id int false sequelizeでデータ取得のときに必須 userId int false firstName nvarchar(50) true lastName nvarchar(50) true createdAt datatime true sequelizeでデータ取得のときに必須 updatedAt datatime true sequelizeでデータ取得のときに必須 - 初期データ
- id:1, firstName:america, lastName:about
- id:2, firstName:amanda, lastName:aldrich
-
-
ログインユーザーを以下の通りに作成
- 参照 http://qiita.com/fkana/items/ae62c7d6c8dd3f425c79
- ユーザー名:kirinuser
- パスワード:kirinuser99
3. アプリの中身を作る
*1.で作成したアプリの以下のファイルを修正していく
-
app.jsでアクセスするURLをつけてあげる(以下を追記
var csdata = require('./routes/sample'); app.use('/sample', sample);
-
modelsフォルダ直下にuser.jsを作成し、以下のコードを書く(DBのテーブル名にはsがつきますが、ここではつきません。Sequelizeの仕様みたいです)
module.exports = function(sequelize, DataTypes) { var User = sequelize.define("User", { userId: DataTypes.INTEGER, firstName: DataTypes.STRING, lastName: DataTypes.STRING }); return User; };
-
modelsフォルダ直下にindex.jsを作成し、以下のコードを書く
"use strict"; var fs = require("fs"); var path = require("path"); var Sequelize = require("sequelize"); var env = process.env.NODE_ENV || "development"; var config = require(path.join(__dirname, '..', 'config', 'config.json'))[env]; var sequelize = new Sequelize(config.database, config.username, config.password, config); var db = {}; fs .readdirSync(__dirname) .filter(function (file) { return (file.indexOf(".") !== 0) && (file !== "index.js"); }) .forEach(function (file) { var model = sequelize.import(path.join(__dirname, file)); db[model.name] = model; }); Object.keys(db).forEach(function (modelName) { if ("associate" in db[modelName]) { db[modelName].associate(db); } }); db.sequelize = sequelize; db.Sequelize = Sequelize; module.exports = db;
-
routes直下にsample.jsというDBに接続する以下のコードを作成
var express = require('express'); var router = express.Router(); var models = require('./../models'); router.get('/', function(req, res) { models.User.findAll().then(function(users) { res.json(users); }) }); module.exports = router;
-
アプリ直下にconfigフォルダを作成し、その直下にconfig.jsonファイルを作成。以下のコードを記述
{ "development": { "username": "kirinuser", "password": "kirinuser99", "database": "kirin", "host": "localhost", "dialect": "mssql" } }
-
最後にブラウザでアクセスして、値が取得できていることを確認する
- localhost:3000/user
- JSON形式のデータが画面上に返ってくるはずです