Skip to content

Commit 1dc81da

Browse files
committed
Initial commit of v0.1
0 parents  commit 1dc81da

File tree

18 files changed

+525
-0
lines changed

18 files changed

+525
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
temp/

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright 2013 Brian Viveiros
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Express + MongoDB REST API Generator [![Build Status](https://secure.travis-ci.org/brianviveiros/generator-express-rest.png?branch=master)](https://travis-ci.org/brianviveiros/generator-express-rest)
2+
3+
A [Yeoman](http://yeoman.io) generator for building a REST API in minutes using Express + MongoDB.
4+
5+
6+
7+

angularUtil.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict';
2+
var path = require('path');
3+
var fs = require('fs');
4+
5+
6+
module.exports = {
7+
rewrite: rewrite,
8+
rewriteFile: rewriteFile
9+
};
10+
11+
function rewriteFile (args) {
12+
args.path = args.path || process.cwd();
13+
var fullPath = path.join(args.path, args.file);
14+
15+
args.haystack = fs.readFileSync(fullPath, 'utf8');
16+
var body = rewrite(args);
17+
18+
fs.writeFileSync(fullPath, body);
19+
}
20+
21+
function escapeRegExp (str) {
22+
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
23+
}
24+
25+
function rewrite (args) {
26+
// check if splicable is already in the body text
27+
var re = new RegExp(args.splicable.map(function (line) {
28+
return '\s*' + escapeRegExp(line);
29+
}).join('\n'));
30+
31+
if (re.test(args.haystack)) {
32+
return args.haystack;
33+
}
34+
35+
var lines = args.haystack.split('\n');
36+
37+
var otherwiseLineIndex = 0;
38+
lines.forEach(function (line, i) {
39+
if (line.indexOf(args.needle) !== -1) {
40+
otherwiseLineIndex = i;
41+
}
42+
});
43+
44+
var spaces = 0;
45+
while (lines[otherwiseLineIndex].charAt(spaces) === ' ') {
46+
spaces += 1;
47+
}
48+
49+
var spaceStr = '';
50+
while ((spaces -= 1) >= 0) {
51+
spaceStr += ' ';
52+
}
53+
54+
lines.splice(otherwiseLineIndex, 0, args.splicable.map(function (line) {
55+
return spaceStr + line;
56+
}).join('\n'));
57+
58+
return lines.join('\n');
59+
}

app/index.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'use strict';
2+
var util = require('util');
3+
var path = require('path');
4+
var yeoman = require('yeoman-generator');
5+
6+
var ExpressRestGenerator = module.exports = function ExpressRestGenerator(args, options, config) {
7+
yeoman.generators.Base.apply(this, arguments);
8+
9+
this.on('end', function () {
10+
this.installDependencies({ skipInstall: options['skip-install'] });
11+
});
12+
13+
this.pkg = JSON.parse(this.readFileAsString(path.join(__dirname, '../package.json')));
14+
};
15+
16+
util.inherits(ExpressRestGenerator, yeoman.generators.Base);
17+
18+
ExpressRestGenerator.prototype.askFor = function askFor() {
19+
var cb = this.async();
20+
21+
// have Yeoman greet the user.
22+
console.log(this.yeoman);
23+
24+
var prompts = [{
25+
name: 'ip',
26+
message: 'IP address where MongoDB is running?',
27+
default: '127.0.0.1'
28+
},
29+
{
30+
name: 'db',
31+
message: 'Database name?',
32+
default: 'myApp'
33+
}];
34+
35+
this.prompt(prompts, function (props) {
36+
this.ip = props.ip;
37+
this.db = props.db;
38+
39+
cb();
40+
}.bind(this));
41+
};
42+
43+
ExpressRestGenerator.prototype.app = function app() {
44+
// From the Express generator
45+
this.mkdir('public');
46+
this.mkdir('public/images');
47+
this.mkdir('public/javascripts');
48+
this.mkdir('public/stylesheets');
49+
this.mkdir('routes');
50+
this.mkdir('views');
51+
52+
this.copy('_package.json', 'package.json');
53+
this.copy('_app.js', 'app.js');
54+
this.copy('_style.css', 'public/stylesheets/style.css');
55+
this.copy('_index.js', 'routes/index.js');
56+
this.copy('_index.jade', 'views/index.jade')
57+
this.copy('_layout.jade', 'views/layout.jade');
58+
59+
// Adding these
60+
this.mkdir('lib')
61+
this.mkdir('resources');
62+
63+
this.copy('_db.js', 'lib/db.js');
64+
this.copy('_log.js', 'lib/log.js');
65+
};
66+
67+
ExpressRestGenerator.prototype.projectfiles = function projectfiles() {
68+
69+
};
70+

app/templates/_app.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
/**
3+
* Module dependencies.
4+
*/
5+
6+
var express = require('express');
7+
var routes = require('./routes');
8+
var http = require('http');
9+
var path = require('path');
10+
11+
var logger = require('./lib/log');
12+
var db = require('./lib/db');
13+
14+
var app = express();
15+
16+
// all environments
17+
app.set('port', process.env.PORT || 3000);
18+
app.set('views', path.join(__dirname, 'views'));
19+
app.set('view engine', 'jade');
20+
app.use(express.favicon());
21+
app.use(express.logger('dev'));
22+
app.use(express.bodyParser());
23+
app.use(express.methodOverride());
24+
app.use(app.router);
25+
app.use(express.static(path.join(__dirname, 'public')));
26+
27+
// development only
28+
if ('development' == app.get('env')) {
29+
app.use(express.errorHandler());
30+
}
31+
32+
app.get('/', routes.index);
33+
34+
// begin resources
35+
36+
// end resources
37+
38+
http.createServer(app).listen(app.get('port'), function(){
39+
console.log('Express server listening on port ' + app.get('port'));
40+
});

app/templates/_db.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Create a Mongoose Connection and define the data models.
3+
*/
4+
5+
var mongoose = require('mongoose');
6+
var logger = require('./log');
7+
8+
// Connect to mongo
9+
var mongoURL = 'mongodb://<%= ip %>/<%= db %>';
10+
mongoose.connect(mongoURL);
11+
12+
var db = mongoose.connection;
13+
db.on('error', console.error.bind(console, 'connection error:'));
14+
db.once('open', function callback () {
15+
logger.info("mongoose connection is open");
16+
});
17+
18+
/*
19+
* The Models
20+
*/
21+
22+
23+
// begin models
24+
// end models

app/templates/_index.jade

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extends layout
2+
3+
block content
4+
h1= title
5+
p Welcome to #{title}

app/templates/_index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
/*
3+
* GET home page.
4+
*/
5+
6+
exports.index = function(req, res){
7+
res.render('index', { title: 'Express' });
8+
};

app/templates/_layout.jade

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
doctype 5
2+
html
3+
head
4+
title= title
5+
link(rel='stylesheet', href='/stylesheets/style.css')
6+
body
7+
block content

app/templates/_log.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// log.js
2+
//
3+
var logger = require('winston');
4+
var fs = require('fs')
5+
var path=require('path');
6+
7+
// Create the directory if it doesn't already exist
8+
var logs_path = path.resolve(__dirname, "../logs/");
9+
if (!fs.existsSync(logs_path)) {
10+
fs.mkdirSync(logs_path);
11+
}
12+
13+
// Default to development.log unless otherwise specified
14+
// in the NODE_ENV environment variable.
15+
var logfile = __dirname + '/../logs/development.log';
16+
if (process.env.NODE_ENV == 'production') {
17+
logfile = __dirname + '/../logs/production.log';
18+
}
19+
20+
// Add the the file logger
21+
logger.add(logger.transports.File, {filename: logfile});
22+
23+
module.exports = logger

app/templates/_package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "application-name",
3+
"version": "0.0.1",
4+
"private": true,
5+
"scripts": {
6+
"start": "node app.js"
7+
},
8+
"dependencies": {
9+
"express": "3.4.2",
10+
"jade": "*",
11+
"winston": "~0.7.2",
12+
"mongoose": "~3.6.20"
13+
}
14+
}

app/templates/_style.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
body {
2+
padding: 50px;
3+
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
4+
}
5+
6+
a {
7+
color: #00B7FF;
8+
}

package.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "generator-express-rest",
3+
"version": "0.1.0",
4+
"description": "Build a REST API in minutes using Express + MongoDB",
5+
"keywords": [
6+
"yeoman-generator"
7+
],
8+
"homepage": "https://github.com/brianviveiros/generator-express-rest",
9+
"bugs": "https://github.com/brianviveiros/generator-express-rest/issues",
10+
"author": {
11+
"name": "Brian Viveiros",
12+
"email": "[email protected]",
13+
"url": "https://github.com/brianviveiros"
14+
},
15+
"main": "app/index.js",
16+
"repository": {
17+
"type": "git",
18+
"url": "git://github.com/brianviveiros/generator-express-rest.git"
19+
},
20+
"scripts": {
21+
"test": "mocha"
22+
},
23+
"dependencies": {
24+
"yeoman-generator": "~0.13.0"
25+
},
26+
"devDependencies": {
27+
"mocha": "~1.12.0"
28+
},
29+
"peerDependencies": {
30+
"yo": ">=1.0.0-rc.1"
31+
},
32+
"engines": {
33+
"node": ">=0.8.0",
34+
"npm": ">=1.2.10"
35+
},
36+
"licenses": [
37+
{
38+
"type": "MIT"
39+
}
40+
]
41+
}

0 commit comments

Comments
 (0)