Skip to content

Commit e762e16

Browse files
committed
1. Retab.
2. Cahnge js style. 3. Support cluster.
1 parent 87b8d71 commit e762e16

File tree

23 files changed

+2108
-536
lines changed

23 files changed

+2108
-536
lines changed

app.js

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,59 @@
1-
/**
2-
* Module dependencies.
3-
*/
4-
5-
var express = require('express')
6-
, mongoose = require('mongoose')
7-
, http = require('http')
8-
, config = require('./config');
9-
10-
var app = express();
11-
12-
mongoose.connect('mongodb://localhost/' + config.DB_NAME);
13-
14-
app.configure(function(){
15-
app.set('port', process.env.PORT || 3000);
16-
app.use(express.favicon());
17-
app.use(express.logger('dev'));
18-
app.use(express.bodyParser());
19-
app.use(express.methodOverride());
20-
app.use(app.router);
21-
app.use(express.static(__dirname + '/public'));
22-
});
23-
24-
app.configure('development', function(){
25-
app.use(express.errorHandler());
26-
});
27-
28-
// config routers
29-
['post'].forEach(function (route) {
30-
require('./routes/' + route)(app);
31-
});
32-
33-
http.createServer(app).listen(app.get('port'), function(){
34-
console.log("Express server listening on port " + app.get('port'));
35-
});
1+
var cluster = require('cluster'),
2+
config = require('./config'),
3+
numCPUs = require('os').cpus().length;
4+
5+
if (cluster.isMaster) {
6+
var i;
7+
// Fork workers.
8+
for (i = 0; i < numCPUs; i++) {
9+
cluster.fork();
10+
}
11+
12+
cluster.on('exit', function(worker, code, signal) {
13+
console.info('Workerer #' + worker.id, 'with pid', worker.process.pid, 'died');
14+
});
15+
16+
} else {
17+
18+
/**
19+
* Module dependencies.
20+
*/
21+
22+
var express = require('express'),
23+
mongoose = require('mongoose'),
24+
http = require('http'),
25+
config = require('./config'),
26+
app = express(),
27+
server;
28+
29+
if (config.DB_USER && config.DB_PASSWORD) {
30+
mongoose.connect('mongodb://' + config.DB_USER + ':' + config.DB_PASSWORD + '@localhost/' + config.DB_NAME);
31+
} else {
32+
mongoose.connect('mongodb://localhost/' + config.DB_NAME);
33+
}
34+
35+
mongoose.connect('mongodb://localhost/' + config.DB_NAME);
36+
37+
app.configure(function() {
38+
app.set('port', config.PORT || 3000);
39+
app.use(express.favicon());
40+
app.use(express.logger('dev'));
41+
app.use(express.bodyParser());
42+
app.use(express.methodOverride());
43+
app.use(app.router);
44+
app.use(express.static(__dirname + '/public'));
45+
});
46+
47+
app.configure('development', function() {
48+
app.use(express.errorHandler());
49+
});
50+
51+
// config routers
52+
['post'].forEach(function (route) {
53+
require('./routes/' + route)(app);
54+
});
55+
56+
server = app.listen(app.get('port'), function() {
57+
console.info("Worker #" + cluster.worker.id, "with pid", cluster.worker.process.pid, "listening on port", app.get('port'));
58+
});
59+
}

config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module.exports = {
2-
DB_NAME: 'backbone-sample-blog'
3-
, API: '/api/v1'
2+
DB_NAME: 'node-backbone-require-sample-blog',
3+
API: '/api/v1'
44
};

config/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
PORT: 8000,
3+
DB_NAME: 'ntugsa-dev',
4+
DB_USER: null,
5+
DB_PASSWORD: null,
6+
API: '/api/v1'
7+
};

models/post.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
var mongoose = require('mongoose')
2-
, PostSchema = require('./schemas').PostSchema
3-
, PostModel = mongoose.model('Post', PostSchema);
1+
var mongoose = require('mongoose'),
2+
PostSchema = require('./schemas').PostSchema,
3+
PostModel = mongoose.model('Post', PostSchema);
44

55
module.exports = PostModel;

models/schemas.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
var mongoose = require('mongoose')
2-
, Schema = mongoose.Schema
3-
, ObjectId = Schema.ObjectId;
1+
var mongoose = require('mongoose'),
2+
Schema = mongoose.Schema,
3+
ObjectId = Schema.ObjectId,
4+
PostSchema;
45

5-
var PostSchema = new Schema({
6-
title: String
7-
, author: String
8-
, body: String
9-
, date: {
10-
type: Date
11-
, default: Date.now
12-
}
6+
PostSchema = new Schema({
7+
title: String,
8+
author: String,
9+
body: String,
10+
date: {
11+
type: Date,
12+
default: Date.now
13+
}
1314
});
1415

1516
module.exports.PostSchema = PostSchema;

package.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
{
2-
"name": "application-name"
2+
"name": "Node-Backbone-Require Example Blog"
33
, "version": "0.0.1"
4+
, "author": "Chun-Yi Liu <[email protected]>"
45
, "private": true
56
, "scripts": {
67
"start": "node app"
78
}
89
, "dependencies": {
9-
"express": "3.0.0beta4"
10-
, "mongoose": "*"
11-
, "underscore": "*"
10+
"express": "3.1.2"
11+
, "connect": "*"
12+
, "mongoose": "*"
13+
, "underscore": "*"
14+
, "async": "*"
15+
}
16+
, "engines": {
17+
"node": "0.10.4"
1218
}
1319
}

public/index.html

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
<!DOCTYPE HTML>
22
<html lang="en">
33
<head>
4-
<meta charset="UTF-8">
5-
<title>Node-Backbone Sample Blog</title>
4+
<meta charset="UTF-8">
5+
<title>Node-Backbone Sample Blog</title>
66

7-
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
8-
<!--[if lt IE 9]>
7+
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
8+
<!--[if lt IE 9]>
99
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
1010
<![endif]-->
1111

12-
<!-- Le styles -->
13-
<link rel="stylesheet" href="/stylesheets/style.css" />
12+
<!-- Le styles -->
13+
<link rel="stylesheet" href="/stylesheets/style.css" />
1414
</head>
1515
<body>
1616

17-
<header>
18-
</header>
17+
<header>
18+
</header>
1919

20-
<div class="container">
20+
<div class="container">
2121

22-
<section class="page-content"></section>
22+
<section class="page-content"></section>
2323

24-
</div> <!-- /container -->
24+
</div> <!-- /container -->
2525

26-
<footer>
27-
</footer>
26+
<footer>
27+
</footer>
2828

29-
<!-- Le javascript
29+
<!-- Le javascript
3030
================================================== -->
31-
<!-- Placed at the end of the document so the pages load faster -->
32-
<script data-main="/javascripts/main" src="/javascripts/lib/require.min.js"></script>
31+
<!-- Placed at the end of the document so the pages load faster -->
32+
<script data-main="/javascripts/main" src="/javascripts/lib/require.min.js"></script>
3333

3434
</body>
3535
</html>

public/javascripts/app.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
define('app', [
2-
'jquery'
3-
, 'underscore'
4-
, 'backbone'
5-
, 'router'
6-
], function ($, _, backbone, router) {
7-
var initialize = function () {
8-
var app = new router();
9-
backbone.history.start();
10-
}
11-
12-
// TODO: error handling with window.onerror
13-
// http://www.slideshare.net/nzakas/enterprise-javascript-error-handling-presentation
2+
'jquery',
3+
'underscore',
4+
'backbone',
5+
'router'
6+
], function($, _, backbone, router) {
7+
var initialize = function() {
8+
var app = new router();
9+
backbone.history.start();
10+
}
1411

15-
return {
16-
initialize: initialize
17-
};
12+
// TODO: error handling with window.onerror
13+
// http://www.slideshare.net/nzakas/enterprise-javascript-error-handling-presentation
14+
15+
return {
16+
initialize: initialize
17+
};
1818
});
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
define('PostCollection', [
2-
'jquery'
3-
, 'underscore'
4-
, 'backbone'
5-
, 'PostModel'
2+
'jquery',
3+
'underscore',
4+
'backbone',
5+
'PostModel'
66
], function ($, _, backbone, PostModel) {
7-
var PostClooection;
7+
var PostClooection;
88

9-
PostCollection = backbone.Collection.extend({
10-
model: PostModel
11-
, url: '/api/v1/post/list'
12-
});
9+
PostCollection = backbone.Collection.extend({
10+
model: PostModel,
11+
url: '/api/v1/post/list'
12+
});
1313

14-
return PostCollection;
14+
return PostCollection;
1515
});

0 commit comments

Comments
 (0)