/nodejs-tutorial-rest-api

:clipboard: This is an app to practise working with Representational State Transfer (REST) APIs that just transfer data instead of user interfaces.

Primary LanguageJavaScript

⚡ Nodejs Tutorial Rest API

  • This is an app to practise working with Representational State Transfer (REST) APIs that just transfer data instead of user interfaces.

  • Note: to open web links in a new window use: ctrl+click on link

GitHub repo size GitHub pull requests GitHub Repo stars GitHub last commit

📄 Table of contents

📚 General info

  • This app creates simple get and post routes/controllers to send and receive data, as well as header and status information.
  • A Postman API and a Codepen REST API test app are used to send/receive data.

📷 Screenshots

Example screenshot

📶 Technologies

💾 Setup

  • Create MongoDB Atlas Cloud database (or local installed MongoDB database) and add user access/database credentials to a new .env file - see .env.example - referred to in app.js.
  • Add IP address to MongoDB Atlas Network Access whitelist. Or simply whitelist all (IP address 0.0.0.0/0).
  • Run npm start for a dev server. Navigate to http://localhost:8080/. The app will automatically reload if you change any of the source files.

💻 Code Examples

  • getPosts method from controllers\feed.js
exports.getPosts = async (req, res, next) => {
  const currentPage = req.query.page || 1;
  const perPage = 2;
  try {
    const totalItems = await Post.find().countDocuments();
    const posts = await Post.find()
      .populate('creator')
      .sort({ createdAt: -1 })
      .skip((currentPage - 1) * perPage)
      .limit(perPage);

        // return a response with json data
    res.status(200).json({
      message: 'Fetched posts successfully.',
      posts: posts,
      totalItems: totalItems
    });
  } catch (err) {
    if (!err.statusCode) {
      err.statusCode = 500;
    }
    next(err);
  }
};

🆒 Features

  • None

📋 Status & To-Do List

  • Status: Part complete & working, Connects to database but nothing viewed on localhost:8080. localhost:8080/graphql works
  • To-Do: Complete code or replace. Also requires more commenting and screen shots.

👏 Inspiration

📁 License

  • N/A

✉️ Contact