-
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
- 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.
- multer v1 is the middleware used for uploading files.
- socket.io v2 is added for real-time event-based communications.
- graphql v14 is added, a query language for APIs.
- bcrypt v2 library used to hash passwords.
- mongoose v5 object modelling for node.js.
- VS Extension Thunder client REST Client for Testing APIs
- Codepen 'REST API test' used to test the app.
- 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 inapp.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 tohttp://localhost:8080/
. The app will automatically reload if you change any of the source files.
- 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);
}
};
- None
- 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.
- N/A
- Repo created by ABateman, email: gomezbateman@yahoo.com