├── .npmignore ├── .gitignore ├── template ├── index.html └── server ├── package.json ├── LICENSE ├── bin └── serverhere ├── README.md └── index.js /.npmignore: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /test 3 | -------------------------------------------------------------------------------- /template/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | <%= cwd %> 6 | <% if (watch) {%><% } %> 7 | 8 | 9 |

It works !

10 | 11 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "serverhere", 3 | "version": "0.1.2", 4 | "description": "Init express in current director as a convenient static server. ", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "npm test" 8 | }, 9 | "bin": { 10 | "serverhere": "bin/serverhere" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/switer/serverhere.git" 15 | }, 16 | "keywords": [ 17 | "express", 18 | "static", 19 | "server" 20 | ], 21 | "author": "switer", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/switer/serverhere/issues" 25 | }, 26 | "homepage": "https://github.com/switer/serverhere", 27 | "dependencies": { 28 | "commander": "^2.5.0", 29 | "ejs": "^1.0.0", 30 | "inquirer": "^0.8.0", 31 | "mkdirp": "^0.5.0" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 guankaishe 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /bin/serverhere: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | var meta = require('../package.json') 3 | var program = require('commander') 4 | var serverhere = require('../index') 5 | 6 | program 7 | .version(meta.version) 8 | .usage('[options] \n\n (path default is ".")\n') 9 | .option('-p --port ', 'server listen port, default to 1024') 10 | .option('-c --compress', 'enable gzip') 11 | .option('-e --engine ', "view's engine ejs/jade/hogan or any others, default not set") 12 | .option('-v --views ', 'views director path, default to "views"') 13 | .option('-w --watch ', 'watch a dir and reload browser when files changed') 14 | .option('-i --ignore ', 'unwatch file/dir under the watch dir, use "," as separator of multiple file') 15 | .parse(process.argv) 16 | 17 | var options = monkeyPicks(program, 18 | 'engine', 'views', 'port', 'compress', 'watch', 'ignore' 19 | ) 20 | options['public'] = program.args[0] || '.' 21 | serverhere(program.args[0] || '.', options) 22 | 23 | /** 24 | * Pick fileds from an object or set to null 25 | **/ 26 | function monkeyPicks (obj) { 27 | var args = Array.prototype.slice.call(arguments) 28 | var nObj = {} 29 | args.shift() 30 | args.forEach(function (key) { 31 | nObj[key] = obj[key] || null 32 | }) 33 | return nObj 34 | } 35 | -------------------------------------------------------------------------------- /template/server: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var express = require('express') 4 | var path = require('path') 5 | var app = express() 6 | var colors = require('colors') 7 | <% if (watch) {%> 8 | /** 9 | * LiveReload 10 | **/ 11 | var livereload = require('express-livereload') 12 | livereload(app, { 13 | <% if (ignore) {%> 14 | exclusions: ['<%- ignore.split(',').join("', '") %>'], 15 | <% } %> 16 | watchDir: path.join(__dirname, '<%- watch || "public" %>') 17 | }) 18 | app.get('/livereload', function (req, res) { 19 | res.send("document.write(''.gray) 50 | <% } %> 51 | }) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | serverhere 2 | ========= 3 | ![logo](http://switer.qiniudn.com/assitant.png) 4 | 5 | Create an express static server in current director with some options, includes gzip/livereload/view engine/port. 6 | If you thinks another something is useful and helpful, please tell me. 7 | 8 | ## Installing 9 | ```bash 10 | npm install serverhere -g 11 | ``` 12 | or 13 | ```bash 14 | npm install ser -g 15 | ``` 16 | 17 | **ser** is a shortcut alisa for **serverhere** 18 | 19 | ## Example 20 | 21 | Following command create a server 22 | - that watch `./public` for livereload 23 | - and enable `gzip` 24 | - and use `jade` as view engine 25 | - and default set `port` to 2014 26 | - and use `./public` as static folder 27 | 28 | ```bash 29 | serverhere --watch public --compress -e jade --port 2014 public 30 | ``` 31 | Before running, you need to install some dependencies: 32 | ``` 33 | npm install 34 | ``` 35 | Then run command to start your server: 36 | ``` 37 | npm start 38 | ``` 39 | 40 | ## Usage 41 | ```cli 42 | Usage: serverhere [options] 43 | 44 | (path default is ".") 45 | 46 | 47 | Options: 48 | 49 | -h, --help output usage information 50 | -V, --version output the version number 51 | -p --port server listen port, default to 1024 52 | -c --compress enable gzip 53 | -e --engine view's engine ejs/jade/hogan or any others, default not set 54 | -v --views views director path, default to "views" 55 | -w --watch watch a dir and reload browser when files changed 56 | -i --ignore unwatch file/dir under the watch dir, use "," as separator of multiple file 57 | ``` 58 | 59 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var fs = require('fs') 3 | var Path = require('path') 4 | var ejs = require('ejs') 5 | var inquirer = require('inquirer') 6 | var mkdirp = require('mkdirp') 7 | var templates = { 8 | "server": fs.readFileSync(Path.join(__dirname, './template/server'), 'utf-8'), 9 | "index": fs.readFileSync(Path.join(__dirname, './template/index.html'), 'utf-8'), 10 | } 11 | 12 | module.exports = function(path, options) { 13 | options.port = options.port || 1024 14 | if (options.engine)!options.views && (options.views = 'views') 15 | 16 | 17 | var serverPath = 'server.js' 18 | var packagePath = 'package.json' 19 | var indexPath = Path.join(options.public, 'index.html') 20 | var existPackage = fs.existsSync(packagePath) 21 | var existIndex = fs.existsSync(indexPath) 22 | 23 | /** 24 | * write static folder and default index.html 25 | */ 26 | if (options.public !== '.') mkdirp.sync(options.public); 27 | options.cwd = Path.basename(process.cwd()) || '' 28 | 29 | if (!existIndex) { 30 | fs.writeFileSync(indexPath, ejs.render(templates.index, options), 'utf-8') 31 | console.log('Done, index.html is created!') 32 | } 33 | 34 | /** 35 | * set package.json 36 | */ 37 | var pjson 38 | if (existPackage) { 39 | try { 40 | pjson = (pjson = fs.readFileSync('package.json', 'utf-8')) ? JSON.parse(pjson) : {} 41 | } catch (e) { 42 | console.log('Your "package.json" file has syntax error : ' + e) 43 | return process.exit(1) 44 | } 45 | } else { 46 | pjson = {} 47 | } 48 | 49 | pjson.name === undefined && (pjson.name = options.cwd) 50 | pjson.scripts = pjson.scripts || {} 51 | pjson.scripts.start = 'node server' 52 | pjson.dependencies = pjson.dependencies || {} 53 | 54 | // custom dependencies 55 | var deps = ['express', 'colors'] 56 | if (options.compress) deps.push('compression') 57 | if (options.engine) deps.push(options.engine) 58 | if (options.watch) deps.push('express-livereload') 59 | 60 | deps.forEach(function(dep) { 61 | pjson.dependencies[dep] = '*' 62 | }) 63 | 64 | fs.writeFileSync('package.json', JSON.stringify(pjson, null, '\t')) 65 | 66 | /** 67 | * write server.js file 68 | */ 69 | var questions = [] 70 | var writeServer = true 71 | var existServer = fs.existsSync(serverPath) 72 | var serverFile = ejs.render(templates.server, options) 73 | 74 | if (existServer) { 75 | questions.push({ 76 | type: "confirm", 77 | name: "writeServer", 78 | message: "server.js is already exist, still rewrite?", 79 | default: true 80 | }) 81 | inquirer.prompt(questions, function(answers) { 82 | existServer && (writeServer = answers.writeServer) 83 | writeServerFile() 84 | }) 85 | } else { 86 | writeServerFile() 87 | } 88 | 89 | function writeServerFile() { 90 | if (writeServer) { 91 | console.log('\nDone, server.js is created!') 92 | fs.writeFileSync('server.js', serverFile, 'utf-8') 93 | } 94 | console.log('Done, ' + (existPackage ? 'package.json is updated!' : 'package.json is created!')) 95 | console.log('\nPlease use "npm install && npm start" to run server.') 96 | } 97 | } 98 | --------------------------------------------------------------------------------