daily pastebin goal
64%
SHARE
TWEET

counter-server.js

beelzebielsk Nov 19th, 2017 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const bp = require('body-parser');
  3. const fs = require('fs');
  4. const app = express();
  5. const counterFile = 'counter.json';
  6.  
  7. // Setting these causes express to use body-parser on your behalf. You
  8. // do not personally use body-parser from here on in.
  9. app.use(bp.urlencoded({
  10.     extended: false
  11. }))
  12. app.use(bp.json());
  13.  
  14. /* When you try to read a file that does not exist, then an error
  15.  * happens. That error is reported to you as an object in the `err`
  16.  * parameter of the readFile callback. One of the properties of this
  17.  * object is 'code', which represents the error code. When the file
  18.  * does not exist, the error code is "ENOENT".
  19.  *
  20.  * I know this because I let the code run without checking for this
  21.  * condition (and without a counter file, to make sure the error
  22.  * happened) and just looked at the error that was printed out.  
  23.  *
  24.  * You might wonder why I turned this into a function, since it is so
  25.  * short. For documentation, honestly. It's easier than writing in
  26.  * comments as to what the wrapped code means whenever I use it.
  27.  */
  28. function isFileNotExistfileErr(fileErr) {
  29.     return fileErr.code === "ENOENT";
  30. }
  31.  
  32. app.get('/', (req, res) => {
  33.     fs.readFile(counterFile, 'utf8', (err, data) => {
  34.         /* `err` is an error which might occur as a result of reading
  35.          * the file. Your code needs to either react correctly to the
  36.          * error, or fail. There is at least one error that you should
  37.          * be able to respond to: the error of the file not existing.
  38.          */
  39.         if (err && !isFileNotExistErr(err)) {
  40.             res.statusCode = 500;
  41.             res.end();
  42.             console.error("Read Error:");
  43.             console.error(err);
  44.             return;
  45.         } else {
  46.             if (err && isFileNotExistErr(err)) {
  47.                 obj = {counter : 0};
  48.             } else {
  49.                 obj = JSON.parse(data);
  50.             }
  51.             obj.counter++;
  52.             fs.writeFile(counterFile, JSON.stringify(obj), (err) => {
  53.                 if (err) {
  54.                     res.statusCode = 500;
  55.                     res.end();
  56.                     console.error("Write Error:");
  57.                     console.error(err);
  58.                     return
  59.                 }
  60.                 res.send("Welcome to my site");
  61.             })
  62.         }
  63.     });
  64. });
  65.  
  66. app.post('/reset', (req, res) => {
  67.     let obj = { counter: 0 };
  68.     fs.writeFile(counterFile, JSON.stringify(obj), (err) => {
  69.         if (err) {
  70.             res.statusCode = 500;
  71.             res.end();
  72.             return;
  73.         } else {
  74.             res.send('okay counter is reset to 0');
  75.         }
  76.     });
  77. });
  78.  
  79. app.listen(3000);
RAW Paste Data
Top