Using Node/Express
with Matt Williams and Robert Murray
Exploring Opportunites
If you want to follow along you will need to have NodeJs, Postgres, & Postman installed, if you don't, don't worry!
https://github.com/themattwilliams/developer-week
&&
http://slides.com/themattwilliams/developer-week-2017
Your Turn!
* package.json
* app.js
* routes/
* swords.js
We will need:
Run npm init -y inside the root of a newly created application folder or forked repo.
npm install --save express knex pg body-parser
install NPM modules
We're going to need express, knex, pg, and body-parser.
Note: Be sure to use the --save flag so they are added to package.json.
npm install --save -g nodemon
install NPM modules
We will use nodemon to refresh our server after making changes to our files
"dependencies": {
"body-parser": "^1.15.2",
"express": "^4.14.0",
"knex": "^0.12.6",
"pg": "^6.1.2"
}
install NPM modules
After installing these, your package.json should look something like this
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var swords = require('./routes/swords')
app.use('/api/swords', swords);
app.listen(process.env.PORT || 8080);
console.log('Woot, server started');
Crack open the app.js file to setup our app.
var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
res.status(200).json({ message: 'rawr! you did it!' });
});
module.exports = router
/routes/swords.js
We will start our Node app and then send a request
then http://localhost:8080/api/swords
nodemon app.js
Postman is a tool we can use to test our routes.
Use Postman to make a GET request to http://localhost:8080/api/swords
To make a database, we'll follow the following steps:
1. Create a SQL database
2. Create a swords table
2. Create a knexfile
3. Create a module that connects to our database by reading our knexfile
Coming up after the Break!