私は次のアプリケーション構造を持っています
/routes
- index.js
dbconfig.js
dbresources.js
server.js
1.内容/routes/index.js
exports.index = function(req, res){
console.log("Routes Successfully");
sequelize.query("SELECT * FROM users_tbl").success(function(rows) {
res.format({
json: function() {
res.send(rows);
}
});
// res.jsonp(rows);
}).error(function(error) {
console.log(error);
});
};
2. dbresources.js の内容
module.exports = {
database: {
name: "dbproject",
host: "root",
password: ""
}
}
3. dbconfig.js の内容
var Sequelize = require('sequelize')
, mysql = require('mysql');
config = require("./dbresources");
db = config.database;
var sequelize = new Sequelize(db.name, db.host, db.password, {
dialect: 'mysql'
});
4.server.jsの内容
var express = require('express')
, http = require('http')
, app = express()
, http = require('http')
, routes = require('./routes')
, path = require('path');
app.configure(function() {
app.set('port', process.env.PORT || 5000);
app.use(express.bodyParser());
app.set(express.methodOverride());
app.set(express.router);
app.use(express.static(__dirname + '/src'));
});
app.get('/user', routes.index);
http.createServer(app).listen(app.get('port'), function(){
console.log("\n\n\tNode (Express) server listening on port " + app.get('port'))
});
ノードを使用してアプリを実行しているとき、localhost:5000/user を起動します。次のエラーが表示されます。
ReferenceError: sequelize is not defined