Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs');
- const _ = require('lodash');
- const nextId = table => {
- let id = 1;
- let currentData = JSON.parse(fs.readFileSync('data.json'), 'utf8');
- if (!currentData[table]) {
- return id;
- }
- return currentData[table][currentData[table].length - 1].id + 1;
- };
- const newPost = (author, title, body) => {
- let post = {
- author,
- title,
- body,
- date: new Date().toLocaleString(),
- votes: 1,
- id: nextId('posts')
- };
- let currentData = JSON.parse(fs.readFileSync('data.json', 'utf8'));
- if (!currentData.posts) {
- currentData.posts = [];
- }
- currentData.posts.push(post);
- fs.writeFileSync('data.json', JSON.stringify(currentData));
- return post;
- };
- const newComment = (author, post, comment) => {
- let ncomment = {
- author,
- post,
- comment,
- date: new Date().toLocaleString(),
- id: nextId('comments')
- };
- let currentData = JSON.parse(fs.readFileSync('data.json', 'utf8'));
- if (!currentData.comments) {
- currentData.comments = [];
- }
- currentData.comments.push(ncomment);
- fs.writeFileSync('data.json', JSON.stringify(currentData));
- return ncomment;
- };
- const newUser = (name, email, password) => {
- let user = {
- name,
- email,
- password,
- registration: new Date().toLocaleString(),
- id: nextId('users')
- };
- let currentData = JSON.parse(fs.readFileSync('data.json', 'utf8'));
- if (!currentData.users) {
- currentData.users = [];
- }
- currentData.users.push(user);
- fs.writeFileSync('data.json', JSON.stringify(currentData));
- return user;
- };
- const checkUser = (name, password) => {
- let currentData = JSON.parse(fs.readFileSync('data.json', 'utf8'));
- let users = currentData.users || [];
- return users.find(u => u.name === name && u.password === password);
- };
- const vote = (post, amount) => {
- let currentData = JSON.parse(fs.readFileSync('data.json', 'utf8'));
- let posts = currentData.posts;
- let found = posts.findIndex(p => p.id === post);
- if (found < 0) return;
- currentData.posts[found].votes += amount;
- fs.writeFileSync('data.json', JSON.stringify(currentData));
- }
- const upvotePost = post => {
- vote(post, +1);
- };
- const downvotePost = post => {
- vote(post, -1);
- };
- const getPosts = () => {
- return JSON.parse(fs.readFileSync('data.json', 'utf8')).posts || [];
- }
- const getUsers = () => {
- return JSON.parse(fs.readFileSync('data.json', 'utf8')).users || [];
- }
- const getComments = () => {
- return JSON.parse(fs.readFileSync('data.json', 'utf8')).comments || [];
- }
- const express = require('express');
- const app = express();
- const cors = require('cors');
- const bodyParser = require('body-parser');
- app.use(bodyParser.json());
- app.use(bodyParser.urlencoded({ extended: true }));
- app.use(cors());
- app.set('json spaces', 40);
- app.get('/posts', function (req, res) {
- res.send(getPosts());
- });
- app.get('/users', function (req, res) {
- res.send(getUsers());
- });
- app.get('/comments', function (req, res) {
- res.send(getComments());
- });
- app.post('/addpost', function (req, res) {
- let post = newPost(req.body.author.id, req.body.title, req.body.body);
- res.send(post);
- });
- app.post('/adduser', function (req, res) {
- let user = newUser(req.body.username, req.body.email, req.body.password);
- res.send(user);
- });
- app.post('/addcomment', function (req, res) {
- let comment = newComment(req.body.user, req.body.post, req.body.body);
- res.send(comment);
- });
- app.post('/upvote', function (req, res) {
- let current = upvotePost(req.body.post);
- res.send(current);
- });
- app.post('/downvote', function (req, res) {
- let current = downvotePost(req.body.post);
- res.send(current);
- });
- app.post('/login', function (req, res) {
- let user = checkUser(req.body.username, req.body.password);
- res.send(user);
- });
- app.listen(3000, () => {
- console.log('server listening at localhost:3000');
- });
Add Comment
Please, Sign In to add comment