Last active
December 15, 2015 15:38
-
-
Save IskenHuang/5282879 to your computer and use it in GitHub Desktop.
Quick launch an web server base on expressjsupdate add support CORS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Tutorial | |
* | |
* Step1. install nodejs & npm (download: http://nodejs.org/download/) | |
* | |
* Step2. install express (http://expressjs.com/) | |
* $ sudo npm install express -g | |
* | |
* Step3. $ node app | |
* default port is 3000 | |
* default root is this file(app.js) path | |
* | |
* if you want to change port or root folder you can follow this format | |
* $ node app [YOUR_PORT] [YOUR_ROOT (base on app.js file path)] or | |
* $ node app [YOUR_ROOT (base on app.js file path)] [YOUR_PORT] | |
* | |
* SAMPLE: $ node app 8080 www | |
* $ node app www 8080 | |
* server will start on port 8080 and root folder is 'www' | |
* | |
* SAMPLE2 $ node app 8080 | |
* server will start on port 8080 and root folder is '/' | |
* | |
* SAMPLE3 $ node app www | |
* server will start on port 3000 and root folder is 'www' | |
*/ | |
var express = require('/usr/local/lib/node_modules/express'), //mac default npm path | |
http = require('http'), | |
path = require('path'), | |
myFolder = '/', | |
myDirectory = '', | |
myPort = 3000; | |
if(process.argv.length > 2){ | |
if(!isNaN(process.argv[2])){ | |
myPort = process.argv[2]; | |
}else{ | |
myFolder = process.argv[2]; | |
} | |
} | |
if(process.argv.length > 3){ | |
if(!isNaN(process.argv[3])){ | |
myPort = process.argv[3]; | |
}else{ | |
myFolder = process.argv[3]; | |
} | |
} | |
myDirectory = (myFolder.match(/^(~\/|\/)/i)) ? myFolder : __dirname + '/' + myFolder; | |
var app = express(); | |
app.configure(function(){ | |
app.set('port', process.env.PORT || myPort); | |
app.use(express.favicon()); | |
app.use(express.logger('dev')); | |
app.use(express.static(path.join(__dirname, myFolder))); | |
app.use(express.directory(myDirectory)); | |
}); | |
app.configure('development', function(){ | |
app.use(express.errorHandler()); | |
}); | |
http.createServer(app).listen(app.get('port'), function(){ | |
var display = 'Express server listening on port '; | |
display += app.get('port'); | |
display += ' root folder is "'; | |
display += myDirectory; | |
display += '"'; | |
console.log(display); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment