GithubHelp home page GithubHelp logo

francisrod01 / advancednodeapp Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 1.0 536 KB

Advanced implementations for Nodejs applications running Express

HTML 2.23% JavaScript 97.77%
express-middleware redis-cache threads testing-library nodejs

advancednodeapp's Introduction

AdvancedNodeApp

Advanced code for Nodejs applications using Express.
Author: Francis Rodrigues

Setup environment

  • Run npm install in the root of the project to install server dependencies
  • Change into the client directory and run npm install --legacy-peer-deps
  • Change back into the root of the project and run npm run dev to start the server
  • Access the application at localhost:3000 in your browser

Important: The credentials for the Mongo Atlas DB in dev.js are read only. If you attempt to log in without first adding your own connection string (covered later in the course) you will see an error: [0] MongoError: user is not allowed to do action [insert] on [advnode.users]

Implementing cache with Redis

app.get('/api/blogs', requireLogin, async (req, res) => {
  const redis = require('redis');
  const util = require('util');

  const redisUrl = 'redis://127.0.0.1:6379';
  const client = redis.createClient(redisUrl);

  // Wraps client.get into a Promise.
  client.get = util.promisify(client.get);

  // Do we have any cached data in Redis related to this query.
  const cachedBlogs = await client.get(req.user.id);

  // if yes, then respond to the request right away and return it.
  if (cachedBlogs) {
    console.log('SERVING FROM CACHE..');
    return res.send(JSON.parse(cachedBlogs));
  }

  // if no, we need to respond to request and update our cache
  // to store the data
  const blogs = await Blog.find({ _user: req.user.id });

  console.log('SERVING FROM MONGODB..');
  res.send(blogs);

  client.set(req.user.id, JSON.stringify(blogs));
});

Sources to learn more

  • Starting project for a course on Advanced Node - @Udemy

License

MIT

advancednodeapp's People

Contributors

stephengrider avatar francisrod01 avatar 0x-cygnet avatar danieljwagener avatar

Stargazers

Ta Tran avatar

Watchers

 avatar

Forkers

tatran0195

advancednodeapp's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.