Advertisement
beelzebielsk

jokes-server.js

Dec 3rd, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const app = require('express')();
  2. const sqlite3 = require('sqlite3');
  3. const db = new sqlite3.Database('jokes.db');
  4. const bp = require('body-parser')
  5.  
  6. /* Using the file jokes.js, ExpressJS, and jokes.db, build an application with the following routes:
  7.  
  8. /
  9. /add
  10. /joke
  11.  
  12. / will return an HTML response with a form with attributes method="POST" and action="/add".
  13. The form will have two text inputs, setup and punchline, and a submit button.
  14.  
  15. /add will accept a POST request from the form and enter a new joke into the database, then redirect back to /.
  16.  
  17. how do make a route response a get request ?
  18. app.get makes a route response to a get request
  19.  
  20. /joke will return an HTML response with a random joke.
  21.  
  22. forms take a destination , action ="/add" (this will be in my form)
  23.  
  24. 1. create a file in jokes.js
  25. 2. create routes
  26. 3. figure out the routes
  27. 4. route / create form , two text inputs setup and punchline, then a submit button
  28. 5. the inform from route / i need to use for /add route = go to database then redirect back to the homepage
  29. 6. route / joke will return a random joke from my database, get everything from my db, get a random from my array
  30. 7.
  31.  
  32. Math.random(tableength(#ofrows))
  33. */
  34.  
  35. app.get('/', (req, res) => {
  36.     res.send(`
  37.    
  38.     <form method="POST" action="/add" >
  39.     Setup:<br>
  40.     <input type="text" name="setup" value="setup">
  41.     <br>
  42.     Punchline:<br>
  43.     <input type="text" name="punchline" value="punchline">
  44.     <br><br>
  45.     <input type="submit" value="Submit">
  46.   </form>
  47.    
  48.     `)
  49. });
  50.  
  51. app.post('/add', (req, res) => {
  52. let userSetup = req.body.setup
  53. let userPunchLine = req.body.punchline
  54.   db.post(`
  55.     INSERT INTO jokes (setup, punchline)
  56.     VALUES ('${userSetup}', '${userPunchLine}');
  57.       `, (err, value) => {
  58.         if (err) {
  59.           res.send(err);
  60.         } else {
  61.           res.json(value);
  62.         }
  63.       });
  64. });
  65.  
  66. // app.get('/jokes', (req, res) => {
  67.    
  68. // });
  69.  
  70.  
  71. app.listen(3000);
  72. Add Comment
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement