Software people who know me know I don't like cut an paste programming. If you can refactor common bits of code out of a program, you probably should. Copying code leads to drudgery and errors if you find you have to change all those places where you hit Ctrl-V. So you can imagine how torked I was at the Node.JS common practice of building new code every time you built a new app. (Granted, it's less common than it used to be, but it's still surprisingly common.)
After the first three connect.js apps I wrote, I refactored the common bits out and made a quick "app runner" that read a config file, used its contents to decide what bits of connect.js middleware to use and then call a different JavaScript module... the one with the "real" code in it. After a couple years of refining, I recently released it as SN-App.
If you use connect.js or express.js, you might find it useful. Rather than writing code to explicitly add commonly used middleware to your Connect or Express app, you build a JSON file listing which bits of middleware you want to load and their parameters. SN-App reads this file and adds the appropriate middleware for you.
Here's a simple SN-App config file. Readers familiar with Connect can probably guess what will happen when it's processed:
properties.json:
{
"title": "SampleApp",
"favicon": "static/favicon.ico",
"static": "static",
"listen": {
"port": 80
}
}
When you execute the SN-App application with the command
`sn-app --config file://properties.json`, it loads Connect's "static" middleware to serve static files out of the directory "static" and listens for requests on port 80.
But why use Node if you're just going to serve static files? If you add these lines to the config file, it will turn on the Body Parser middleware and load an additional JavaScript module from src/logic.js:
"bodyParser": true, "source": "src/logic"
And if you're a little lazy, you can ask SN-App to build a skeletal web application for you, complete with a skeletal Bootstrap or Semantic-UI front page. If you wanted to build a simple app to run on port 9001 and served a Semantic-UI index page, just do this:
sn-app-build --title SampleApp --port 9001 --static static \ --css semanticui make sn-app --config file://SampleApp.json
Up and running in seconds flat. If you want to do anything interesting, you'll likely have to edit the SampleApp.json file to do anything interesting, but hey, you were going to do that anyway. SN-App doesn't write the interesting bits for you, it gets the boring bits out of your way so you can get straight to the fun stuff.
Lastly, you can ask SN-App to build a debian init file for you. After running sn-app-build to build the app skeleton and running sn-app to make sure it works, the
sn-app-initgen command will generate an init file suitable for inclusion in >/etc/init.d:sn-app-initgen --name SampleApp --desc "A Sample Application" \ --dir `pwd` > /etc/init.d/sample ln -s `which sn-app` SampleApp chmod 755 /etc/init.d/sample insserv sample # Older debians may have to use update-rc.d instead of insserv.
Installing SN-App is pretty straight-forward as well; just use NPM:
sudo npm install -g sn-app
And with that, you're ready to go. For detailed info about properties file options, see the documentation at https://github.com/smithee-us/sn-app - and as always, ping me with questions or comments.
No comments:
Post a Comment