Skip to content

Instantly share code, notes, and snippets.

@sebdeckers
Last active April 1, 2016 08:49
Show Gist options
  • Save sebdeckers/4ba1bf6e6cfd1f4ca5fb4a5452a6c0fe to your computer and use it in GitHub Desktop.
Save sebdeckers/4ba1bf6e6cfd1f4ca5fb4a5452a6c0fe to your computer and use it in GitHub Desktop.
GA WDI Node.js guest talk outline
{
"name": "express-middleware",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"at-your-service-middleware": "^1.0.0",
"express": "^4.13.4"
}
}
var path = require('path')
var fs = require('fs')
var express = require('express')
var atYourService = require('at-your-service-middleware')
var app = new express()
app.use(atYourService)
app.get('/', function (req, res) {
res.send('Sad world!')
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
var atYourService = function (req, res, next) {
var filepath = path.join(__dirname, 'foobar.txt')
fs.readFile(filepath, function (err, data) {
if (err) next()
res.send(data)
})
}
module.exports = atYourService
{
"name": "at-your-service-middleware",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

Note: This was a vague, a-priori outline. We went over some of this during the talk.

Node.js and its superhero sidekick NPM

JavaScript is just a language. Not inherently useful.

Host environment provides access to the outside world through APIs.

Browser provides APIs for the web. Sandboxed for security. Specs for interoperability.

Node.js offers input and output APIs for file system access (fs) and networking (net). Runs with full user privileges; caveat emptor.

The mechanism in Node.js to access all these APIs is the super nifty require() function.

But wait, there's more! require() also loads JavaScript and JSON files in relative paths or the special node_modules folder. This is how we can structure large apps into multiple, smaller files, or spin-off parts into standalone packages for re-use across multiple apps.

This is where NPM comes in: handling the chaos of dependency management.

Let's refactor the reusable code in our app into a dependency by publishing it to the NPM registry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment