Skip to content

Instantly share code, notes, and snippets.

@franciscoandres
Last active July 11, 2018 20:45
Show Gist options
  • Save franciscoandres/4f54a54ff1f199bd3400b366808f5294 to your computer and use it in GitHub Desktop.
Save franciscoandres/4f54a54ff1f199bd3400b366808f5294 to your computer and use it in GitHub Desktop.

RESTful API

Endpoints

User

Create a new user

POST '/api/users/new'

Article

Create a new article

POST '/api/articles/new'

Edit an article

PUT '/api/articles/:article/edit'

Example

PUT '/api/articles/5b46249f21e14c79bbf2002f/edit'

Delete an article

DELETE '/api/articles/:article/delete'

Example

PUT '/api/articles/5b46249f21e14c79bbf2002f/delete'

Return all articles (from all users) that contains the given tag(s) (1 or more)

GET '/api/articles/search_by_tags/:tags'

Example

GET '/api/articles/search_by_tags/microsoft,apple'

or

GET '/api/articles/search_by_tags/microsoft'

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserModel = new Schema({
name: {
type: String
},
avatar: {
type: String
}
})
const ArticleModel = new Schema({
userId: {
type: Schema.Types.ObjectId, ref: 'UserModel'
},
title: {
type: String
},
text: {
type: String
},
tags: Array
})
module.exports = {
UserModel: mongoose.model('users', UserModel),
ArticleModel: mongoose.model('articles', ArticleModel)
}
const express = require('express');
const app = express();
const port = process.env.PORT || 5656;
const mongoose = require('mongoose');
mongoose.connect("mongodb://localhost:27017/restful");
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var database = mongoose.connection;
database.on("error", console.error.bind(console, "connection error"));
database.once("open", function(callback) {
console.log("Connected!");
});
// Models
const { UserModel, ArticleModel } = require('./Models.js')
// Routes
app.post('/api/users/new', function (request, response) {
const user = new UserModel(request.body)
user.save(function (error) {
if (error) return response.status(500).send(error)
return response.status(201).send(user)
})
})
app.post('/api/articles/new', function (request, response) {
ArticleModel({
userId: '5b46135cc193c0606f50f288', // get from ???
title: request.body.title,
text: request.body.text,
tags: request.body.tags.split(", ")
})
.save(function (error) {
if (error) return response.status(500).send(error)
return response.status(201).send('Created')
})
})
app.put('/api/articles/:article/edit', function (request, response) {
ArticleModel.findByIdAndUpdate(
request.params.article, request.body, { new: true }, function (error, article) {
if (error) return response.status(500).send(error)
return response.status(202).send(article)
})
})
app.delete('/api/articles/:article/delete', function (request, response) {
ArticleModel.findByIdAndRemove(request.params.article, function (error, result) {
if (error) return response.status(500).send(error)
return response.status(202).send('Deleted')
})
})
app.get('/api/articles/search_by_tags/:tags', function (request, response) {
var tagsParams = request.params.tags.split(',')
ArticleModel
.find({ tags: { $in: tagsParams } })
.select('title text tags')
.exec(function (error, result) {
if (error) return response.status(500).send(error)
return response.send(result)
})
})
app.listen(port, () => {
console.log(`http://localhost:${port}`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment