fritter-backend
Science Score: 18.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
○codemeta.json file
-
○.zenodo.json file
-
○DOI references
-
○Academic publication links
-
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (14.0%) to scientific vocabulary
Repository
Basic Info
- Host: GitHub
- Owner: meerameeraonthewall
- Language: TypeScript
- Default Branch: main
- Homepage: fritter-backend-nine-xi.vercel.app
- Size: 121 KB
Statistics
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
- Releases: 0
Metadata Files
README.md
Fritter
Build your own not-quite-Twitter!
Starter Code
This starter code implements users (with login/sessions), and freets so that you may focus on implementing your own design ideas.
The project is structured as follows:
index.tssets up the database connection and the Express server/freetcontains files related to freet conceptcollection.tscontains freet collection class to wrap around MongoDB databasemiddleware.tscontains freet middlewaremodel.tscontains definition of freet datatyperouter.tscontains backend freet routesutil.tscontains freet utility functions for transforming data returned to the client
/usercontains files related to user conceptcollection.tscontains user collection class to wrap around MongoDB databasemiddleware.tscontains user middlewaremodel.ts- contains definition of user datatyperouter.ts- contains backend user routesutil.tscontains user utility functions for transforming data returned to the client
/publiccontains the code for the frontend (HTML/CSS/browser JS)
Installation
Make a copy of this repository under your personal GitHub account by clicking the Use this template button. Make sure to enable the Include all branches option.
If you did not take 6.031 in Fall 2021 or Spring 2022, to ensure that your machine has the necessary software for the assignment, please follow Steps 1, 2, 5, and 6 on this page from the 6.031 website (now 6.1020).
Setting up the demo branch (optional, but very helpful as a reference!)
- Navigate to the root folder of your cloned repository.
- Run
source demo-setup.shto set up the demo branches. - Check your local branches with
git branch; you should have one new branch, with a new commit.view-demodemos how to extend functionality of a resource
- If everything looks good, run
git push --all origin. At this point, you should see the demo branch athttps://github.com/<username>/<repo-name>/branches(and theview-demo-codebranch can now be deleted!) - Now, if you navigate to the commit history of this branch (
https://github.com/<username>/<repo-name>/commits/<branch-name>), you can click on the "demo:" commit and see exactly what we changed for each demo!
MongoDB Atlas setup
Follow the instructions here to add your fritter project to MongoDB Atlas.
After following the instructions above, you should have copied a secret that looks something like mongodb+srv://xxxxxx:xxxxxxxxx@cluster0.yc2imit.mongodb.net/?retryWrites=true&w=majority. Note that this allows complete access to your database, so do not include it anywhere that is pushed to GitHub or any other publicly accessible location.
To allow your local server to connect to the database you just created, create a file named .env in the project's root directory with the contents
MONGO_SRV=mongodb+srv://xxxxxx:xxxxxxxxx@cluster0.yc2imit.mongodb.net/?retryWrites=true&w=majority
where the secret is replaced with the one you obtained.
Post-Installation
After finishing setup, we recommend testing both locally running the starter code, and deploying the code to Vercel, to make sure that both work before you run into issues later. The instructions can be found in the following two sections.
Running your code locally
Firstly, open the terminal or command prompt and cd to the directory for this assignment. Before you make any changes, run the command npm install to install all the packages in package.json. You do not need to run this command every time you make any changes, unless you add a new package to the dependencies in package.json.
Finally, to test your changes locally, run the command npm start in the terminal or command prompt and navigate to localhost:3000 and interact with the frontend to test your routes.
Deployment to Vercel
We will be using Vercel to host a publicly accessible deployment of your application.
Create a fork of this repository through GitHub. This will create a repository in your GitHub account with a copy of the starter code. You'll use this copy to push your work and to deploy from.
Create a Vercel account using your GitHub account.
After you log in, go to the project creation page and select
Continue with GitHuband give Vercel the permissions it asks for.Find the repository you just created and click
Import. For theFramework Preset, chooseOther. In theEnvironment Variablessection, add an entry whereNAMEisMONGO_SRVandVALUEis your MongoDB secret.Click
Deployand you will get a link likehttps://fritter-starter-abcd.vercel.app/where you can access your site.
Vercel will automatically deploy the latest version of your code whenever a push is made to the main branch.
Adding new concepts to Fritter
Backend
The data that Fritter stores is divided into modular collections called "resources". The starter code has only two resources, "freets" and "users". The codebase has the following:
- A model file for each resource (e.g.
freet/model.ts). This defines the resource's datatype, which defines the resource's backend type, and should be a distilled form of the information this resource holds (as in ADTs). This also defines its schema, which tells MongoDB how to store our resource, and should match with the datatype. - A collection file for each resource (e.g.
freet/collection.ts). This defines operations Fritter might want to perform on the resource. Each operation works on the entire database table (represented by e.g.FreetModel), so you would operate on one Freet by usingFreetModel.findOne(). - Routes file (e.g.
freet/router.ts). This contains the Fritter backend's REST API routes for freets resource, and interact with the resource collection. All the routes in the file are automatically prefixed by e.g./api/freets. - Middleware file (e.g
freet/middleware.ts). This contains methods that validate the state of the resource before performing logic for a given API route. For instanceisFreetExistsinfreet/middleware.tsensures that a freet with givenfreetIdexists in the database
To add a resource or edit functionality of an existing resource:
- Create/modify files in the four above categories, making sure you have one model file, one collection one router file, and one middleware file per resource.
- It helps to go in the order that they're listed above, starting with the resource's datatype.
- In
freet/utils.tsanduser/utils.tsthere are type definitions for frontend representations of resources, and functions to convert from a backend resource type. Create/modify these as necessary.- An example: the frontend type definition for User lacks a
passwordproperty, because the frontend should never be receiving users' passwords.
- An example: the frontend type definition for User lacks a
Frontend
For this assignment, we provide an extremely basic frontend for users to interact with the backend. Each box (html form) represents exactly one API route, with a textbox for each parameter that the route takes.
To add a new route to the frontend, two components need to be added: a form in public/index.html and a corresponding event handler in public/scripts/index.js. The form will allow the user to input any necessary fields for the route, and the event handler will take the values of these fields and make an API call to your backend.
For example, the form for the user creation route looks like:
<form id="create-user">
<h3>Create User</h3>
<div>
<label for="username">Username:</label>
<input id="username" name="username">
</div>
<div>
<label for="password">Password:</label>
<input id="password" name="password">
</div>
<input type="submit" value="Create User">
</form>
In public/scripts/user.js, there is an event handler for this form (event handlers are separated in files by concept, currently user.js and freet.js), which makes a POST request to the backend:
function createUser(fields) {
fetch('/api/users', {method: 'POST', body: JSON.stringify(fields), headers: {'Content-Type': 'application/json'}})
.then(showResponse)
.catch(showResponse);
}
Here, fields is a JSON object which contains key/value pairs, where the key is the name associated with the input field in the form and the value is whatever is entered on the frontend. For instance, in the form above, the first input field has name, username, which will be a key in fields object whose value is whatever has been entered as the username on the frontend. Thus, whatever name you attach to any input field is the same name you will can use to access the value entered in that input field from fields object.
To link the form and event handler together, there is an entry in the formsAndHandlers object (the key is the id attribute of the <form> tag and the value is the event handler function) in public/scripts/index.js:
const formsAndHandlers = {
'create-user': createUser,
...
};
MongoDB
MongoDB is how you will be storing the data of your application. It is essentially a document database that stores data as JSON-like objects that have dynamic schema. You can see the current starter code schema in freet/model.ts and user/model.ts.
Mongoose
You will be using Mongoose, a Node.js-Object Data Modeling (ORM) library for MongoDB. This is a NoSQL database, so you aren't constrained to a rigid data model, meaning you can add/remove fields as needed. The application connects to the MongoDB database using Mongoose in index.ts, where you see mongoose.connects(...). After it connects, you will be able to make mongoose queries such as FindOne or deleteMany.
Schemas
In this starter code, we have provided user and freet collections. Each collection has defined schemas in */model.ts. Once you defined a Schema, you must create a Model object out of the schema. The instances of your model are what we call "documents", which is what is stored in collections. Each schema maps to a MongoDB collection and defines the shape and structure of documents in that collection, such as what fields the document has. When a schema is defined, documents in the collection must follow the schema structure. You can read more about documents here.
To create a new Schema, you first need to define an interface which represents the type definition. You can then create a new Schema object by declaring const ExampleSchema = new Schema<Example>(...) where Example is the type definition on the backend. Then, you can create a model like const ExampleModel = model<Example>("Example", ExampleSchema). You can see a more detailed schema in the model.ts files mentioned above.
Validation
Mongoose allows you to use schema validation if you want to ensure that certain fields exist. For example, if you look at freet/model.ts, you will find fields like
content: {
type: String,
required: true
}
within the schema. This tells us that the content field must have type String, and that it is required for documents in that collection. A freet must have a String type value for the content field to be added to the freets collection.
API routes
The following api routes have already been implemented for you (Make sure to document all the routes that you have added.):
GET /
This renders the index.html file that will be used to interact with the backend
GET /api/freets - Get all the freets
Returns
- An array of all freets sorted in descending order by date modified
GET /api/freets?author=USERNAME - Get freets by author
Returns
- An array of freets created by user with username
author
Throws
400ifauthoris not given404ifauthoris not a recognized username of any user
POST /api/freets - Create a new freet
Body
content{string} - The content of the freet
Returns
- A success message
- A object with the created freet
Throws
403if the user is not logged in400If the freet content is empty or a stream of empty spaces413If the freet content is more than 140 characters long
DELETE /api/freets/:freetId? - Delete an existing freet
Returns
- A success message
Throws
403if the user is not logged in403if the user is not the author of the freet404if the freetId is invalid
PUT /api/freets/:freetId? - Update an existing freet
Body
content{string} - The new content of the freet
Returns
- A success message
- An object with the updated freet
Throws
403if the user is not logged in404if the freetId is invalid403if the user is not the author of the freet400if the new freet content is empty or a stream of empty spaces413if the new freet content is more than 140 characters long
POST /api/users/session - Sign in user
Body
username{string} - The user's usernamepassword{string} - The user's password
Returns
- A success message
- An object with user's details (without password)
Throws
403if the user is already logged in400if username or password is not in correct format format or missing in the req401if the user login credentials are invalid
DELETE /api/users/session - Sign out user
Returns
- A success message
Throws
403if user is not logged in
POST /api/users - Create an new user account
Body
username{string} - The user's usernamepassword{string} - The user's password
Returns
- A success message
- An object with the created user's details (without password)
Throws
403if there is a user already logged in400if username or password is in the wrong format409if username is already in use
PUT /api/users - Update a user's profile
Body (no need to add fields that are not being changed)
username{string} - The user's usernamepassword{string} - The user's password
Returns
- A success message
- An object with the update user details (without password)
Throws
403if the user is not logged in400if username or password is in the wrong format409if the username is already in use
DELETE /api/users - Delete user
Returns
- A success message
Throws
403if the user is not logged in
PUT /api/freetreacts/:freetId? - React to a Freet
Body
value{select} - The value of the react
Returns
- A success message
- An object with the updated freet
Throws
400if the user is not logged in404if the freetId is invalid
GET api/freetreacts/:freetId? - View reacts by Freet
Returns
- An array of all the freetreacts associated with the freet with id freetId
Throws
404if freet doesn't exist with freetId
POST api/cite/:freetId? - Add a citation
Body
url{string} - The url to add as a citation
Returns
- A success message
- An object with the new citation
Throws
403if the user is not logged in404if the freetId is invalid400if the body string does not follow the expected URL format403if the user is not the author of the freet
DELETE api/cite/:citationId? - Remove a citation
Returns
- An array of citations
Throws
404if the freet with that ID does not exist
GET api/cite/:freetId? - View citations by freet
Returns
- An array of citations associated with the freet with ID freetId
Throws
404if the citation with that ID does not exist
PUT api/users/scroll/:userId? - Edit scroll settings
Body
mode{select} - The way the user wants Fritter to handle user-specified time limits
Returns
- A success message
- An object with the update user details (without password)
Throws
- 400 if the user is not logged in
Owner
- Login: meerameeraonthewall
- Kind: user
- Repositories: 5
- Profile: https://github.com/meerameeraonthewall
Citation (citation/collection.ts)
import type {Types} from 'mongoose';
import CitationModel from './model';
import type {Citation} from './model';
/**
* Operations:
* addOne
* removeOne
* findOne
*/
class CitationCollection {
/**
* Add a citation to the collection
*
* @param {string} freetId - The id of the freet
* @param {string} url - The url to cite
* @return {Promise<Citation>} - The newly created citation
*/
static async addOne(freetId: Types.ObjectId | string, url: string): Promise<Citation> {
const newCitation = new CitationModel({freetId, url});
await newCitation.save(); // Saves citation to MongoDB
return newCitation;
}
/**
* Remove a citation with given value.
*
* @param {string} citationId - The Id of citation to remove
* @return {Promise<Boolean>} - true if the citation has been deleted, false otherwise
*/
static async removeOne(citationId: Types.ObjectId | string): Promise<boolean> {
const citation = await CitationModel.deleteOne({_id: citationId});
return citation !== null;
}
/**
* Find a citation by citationId
*
* @param {string} citationId - The id of the citation to find
* @return {Promise<Citation> | Promise<null> } - The citation with the given citationId, if any
*/
static async findOne(citationId: Types.ObjectId | string): Promise<Citation> {
return CitationModel.findOne({_id: citationId});
}
/**
* Find all citations by freetId
*/
static async findByFreetId(freetId: Types.ObjectId | string): Promise<Citation[]> {
const citations = await CitationModel.find({freetId});
return citations;
}
}
export default CitationCollection;
GitHub Events
Total
Last Year
Dependencies
- 280 dependencies
- @types/express ^4.17.14 development
- @types/express-session 1.17.0 development
- @types/mongoose ^5.11.97 development
- @types/morgan ^1.9.3 development
- @types/uuid ^8.3.4 development
- @typescript-eslint/eslint-plugin ^5.39.0 development
- @typescript-eslint/parser ^5.39.0 development
- eslint-config-xo ^0.42.0 development
- eslint-config-xo-typescript ^0.53.0 development
- nodemon ^2.0.20 development
- ts-node ^10.6.0 development
- typescript ^4.6.4 development
- debug ~4.3.4
- dotenv ^16.0.2
- eslint ^8.25.0
- express ^4.18.1
- express-handlebars ^6.0.6
- express-session ^1.17.3
- moment ^2.29.4
- mongoose ^6.6.2
- morgan ~1.10.0