Skip to content

Instantly share code, notes, and snippets.

@kylefritz
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save kylefritz/40f25f8d13f45dbb9473 to your computer and use it in GitHub Desktop.

Select an option

Save kylefritz/40f25f8d13f45dbb9473 to your computer and use it in GitHub Desktop.
BEWD Class #10 Lab
  1. follow the commands in boom.sh to get started. What directory should you start in? What does each command do?
  2. Download index.js to start you minimal express app. Run it with node index.js or npm start.
  3. Make a change to index.js, you won't see the results until you restart your app. Install nodemon or similar so that you can get "auto" restarting behavior
  4. Create a database called bewd_twitter using psql
  5. Create a sql directory in your project. Add a file called schema.sql to setup your database schema. In it, add a table to store your tweets. Tweets should have an auto-incrementing primary key (id), a 255 character body, a created_at timestamp.
  6. Add a file called seeds.sql to add some seed data to your app. Add a couple of tweets
  7. Use psql to run these two files.
  8. Make a commit!
  9. Add the pg package to your project to connect Node to Postgres.
  10. In index.js require in the pg package. Inside of the root route, make a connection to postgres and select all of your tweets. Return the response as JSON.
  11. Create a new route at POST /tweets to add a new tweet. Try it out over curl.
  12. Parameterize your sql to add tweets over your api
  13. Move route to get tweets into GET /tweets
  14. Add a public directory to your app and a basic index.html
  15. In your index.html create a form that submits to your /tweets route
  16. Wire up a react app that shows your list of tweets and adds new tweeets
  17. Add a users table to your schema. Add a user_id column to your tweets table. Create some users in your seeds file.
  18. Use the database to enforce that a tweet must have a user. Check out foreign keys
mkdir inclass-10
cd inclass-10
git init
curl https://www.gitignore.io/api/node > .gitignore
npm init
npm install --save dotenv express body-parser
git add -A
git commit -m 'initial commit'
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
require('dotenv').load();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
var port = process.env.PORT || 8000;
var conString = process.env.DATABASE_URL || "postgres://localhost/bewd_twitter";
app.get('/', function (req, res) {
res.send('you made it this far');
});
//boot up the server
app.listen(port);
console.log('The server is listening to ' + port);
@gkbsoftware

Copy link
Copy Markdown
  1. For created_at use the timestamp datatype

@kylefritz

Copy link
Copy Markdown
Author

install the body-parser and other packages from the express starter app with:

$ npm install --save cookie-parser body-parser morgan serve-favicon

then include this code in your index.js file to make use of your new packages

var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
// app.use(express.static(path.join(__dirname, 'public')));

@kylefritz

Copy link
Copy Markdown
Author

curl to post data
$ curl --data "tweet=things are great now" http://localhost:8000/tweets

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