little-bear
is a web framework that base on express.
A simple server looks like:
server/
├── theme.css
├── app.js
├── index.html
└── app.sv.js
app.sv.js:
exports.def = function() {
var path = require('path');
this.get('/', function(req, res, next) {
res.sendFile(path.join(__dirname, 'index.html'));
});
};
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello, world!</title>
<link rel="stylesheet" href="./theme.css">
</head>
<body>
<h1>Hello, world!</h1>
<script src="./app.js"></script>
</body>
</html>
Assuming that the project directory looks like:
project/
├── server/
└── app.js
app.js:
var path = require('path');
var LittleBear = require('little-bear');
var bear = new LittleBear({
root: path.join(__dirname, 'server')
});
bear.run(3000);
You could use LittleBear.static
middleware to serve static files:
exports.def = function() {
var LittleBear = require('little-bear');
this.use(LittleBear.static(__dirname));
};
In static directory, all files are public unless:
node_modules/
directory- has prefix
.
, such as.git/
,.gitignore
- has prefix
_
, such as_views/
- has suffix
.sv.js
or.mod.js
The file that has suffix .sv.js
or .mod.js
will be treated as server script file, LittleBear
will require and run it.
A server script file that has suffix .mod.js
is a module, it must export name
and def
:
exports.name = 'models.Article';
exports.def = function() {
var Article = {};
Article.find = function() {/*...*/};
return Article;
};
If a module has dependencies, it should export deps
:
exports.name = 'someArticles';
exports.deps = {
Article: 'models.Article'
};
exports.def = function(mods) {
return mods.Article.find();
};
LittleBear will set the routes automatically according to the file name.
You could define routes in server script file that has suffix .sv.js
. In these files, the context of def
function is an instance of express.Router
or express.Application
.
server/
├── api/
│ ├── articles.sv.js
│ └── users.sv.js
├── blog/
│ └── index.sv.js
├── about.sv.js
└── index.sv.js
index.sv.js:
exports.def = function() {
// GET /
this.get('/', function(req, res, next) {
res.send('home');
});
};
about.sv.js:
exports.def = function() {
// GET /about
this.get('/', function(req, res, next) {
res.send('about');
});
};
api/articles.sv.js:
exports.deps = {
Article: 'models.Article'
};
exports.def = function(mods) {
// GET /api/articles
this.get('/', function(req, res, next) {
res.json(mods.Article.find());
});
// GET /api/articles/:id
this.get('/:id', function(req, res, next) {
res.json({
id: req.params.id
});
});
};
blog/index.sv.js:
exports.def = function() {
// GET /blog
this.get('/', function(req, res, next) {
res.send('blog');
});
};
If a directory has app.sv.js
file, the directory will be mount as a sub app.
The context of def
function is an instance of express.Application
.
var LittleBear = require('little-bear');
exports.def = function(mods, routes) {
this.engine('jade', require('jade').__express);
this.set('views', rootPath);
this.set('view engine', 'jade');
routes(); // init routes, it must be called
this.use(LittleBear.static(__dirname));
this.use(function(err, req, res, next) {
console.error(err.stack || err);
res.sendStatus(500);
});
};
Arguments
- opts: {Object} - Initial options.
- root: {String} - The absolute path of server directory.
Example
var bear = new LittleBear({
root: path.join(__dirname, 'server')
});
Only init modules and routes.
Return
- {Promise}
Example
var bear = new LittleBear(opts);
bear.init().then(function(app) {
// app is an instance of express.Application
}).catch(console.error);
Init and run LittleBear app.
Arguments
- port: {Number} - The port number that LittleBear app will listen on.
Example
var bear = new LittleBear(opts);
bear.run(3000);
Return a static server middleware.
Arguments
- path: {String} - The absolute path of static directory.
Example
In app.sv.js
:
var LittleBear = require('little-bear');
exports.def = function(mods, routes) {
routes();
this.use(LittleBear.static(__dirname));
};
- Test support