nodeserver
Achieve node server's domain name resolution and web application's router
What is realized
1.This is a simple node http server,achieve a mapping for server and domain name,than a mapping for the static file and API route
2.right response for get, post, put, delete method,then submit the params in route query string and form data to your node backend.
Setup and run
Global module
make sure you have global installed node.
Initialization
$ sudo npm install web-node-server
Configure
You should configure server in this directory, specific method is:
// configure server constant// config.jsexportsconstant = // default port port: 9999 // welcome page's configure host: backend: path frondend: path baseTemp: 'welcome.html' ;// Binding to the domain nameexportsserv = // sites' domain name 'localhost1': // sites' backend workspace backend: '/Users/pg/demo/nodeserver/default/' // sites' frondend workspace frondend: '/Users/pg/demo/nodeserver/default/' // sites' static file entrance baseTemp: 'demo.html' ;
At backend workspace (previously defined workspace) create config.js. Write the site routing information
// project_url/config.js// Define a API's url,all get method's API are in app.url.get object,the key is route url,value is a function which will be running. Than define post, put, delete route.exportsapp = url: 'get': '/api': listget 'post': '/api': listpost 'put': '/api': listput 'delete': '/api': listremove var list = { resstatus = 200; resdata = message: 'yes! this is a response data with get method' routeParams: routeParams getParams: getParams ; return ; // return application/json } { resstatus = 200; reshtml = '<html></html>'; return ; // return text/html } { resstatus = 302; resurl = '/article'; return ; // return 302 to a url }
run
// In root directorynode web-node-serverjs // In project directory (run this directory only, with local config)// you may add web-node-server into you package.jsonconfig = 'localhost': backend: __dirname + '/api/' frondend: __dirname + '/web/' baseTemp: 'index.html' var server = ;serverstartconfig;
Learn more
There are some example in welcome page about get, post, put, delete method's using. You can reference
you can give me your suggestion in github.
The basic file directory structure
If any changes please update this section
├── configjs ... server's domain example configure├── index.js ... example startup code├── nodeserver.js ... define response callback,than start server├── router.js ... distinguish different request, make different treatment├── tool ... gadgets└── default ... default welcome page ├── config.js ... example route configure ├── demo.html ... demo page about a demo configure └── welcome.html ... welcome page,the function demonstration