Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
"env": {
"browser": true,
"commonjs": true,
"es2021": true
},
"parserOptions": {
"ecmaVersion": "latest"
},
"rules": {
}
}
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
.env
.DS_Store

cypress/screenshots
cypress/videos
42 changes: 0 additions & 42 deletions .rubocop.yml

This file was deleted.

84 changes: 59 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,75 @@
Instagram Challenge
===================

## Instructions
This app is a copy of instagram written in Javascript. Express was used to create the server and routes, Mongodb is the database where the data is stored and mongoose was used to connect the two together.

* Feel free to use Google, your notes, books, etc., but work on your own
* If you refer to the solution of another coach or student, please put a link to that in your README
* If you have a partial solution, **still check in a partial solution**
* You must submit a pull request to this repo with your code by 9am Monday morning
All the necessary files for this app have been separated into appropriate folders. The [views](https://github.com/jmcnally17/instagram-challenge/tree/main/views) folder contains the HBS files that contain the HTML code for each page. The [routes](https://github.com/jmcnally17/instagram-challenge/tree/main/routes) folder contains the defined routes for each group of objects that govern HTTP requests throughout the app, with help from the [controllers](https://github.com/jmcnally17/instagram-challenge/tree/main/controllers) folder. Finally, the object classes are defined in the [models](https://github.com/jmcnally17/instagram-challenge/tree/main/models) folder, which use Mongoose to create the necessary Schema which define the classes themselves. These Schema also determine what information gets stored in the database.

## Task
So far, the functionality is limited due to not having a lot of time to work on this project.

Build Instagram: Simple huh!
## Getting Started

Your challenge is to build Instagram using Rails. You'll need **users** who can post **pictures**, write **comments** on pictures and **like** a picture. Style it like Instagram's website (or more awesome).
If you haven't already, install nvm using homebrew:

Bonus if you can add filters!
```
brew install nvm
```

## How to start
Then, open a new terminal and install Node.js:

1. Produce some stories, break them down into tasks, and estimate
2. Fork this repo, clone, etc
3. Initialize a new rails project
```
nvm install node
```

Remember to proceed in small steps! Getting confused? Make the steps even smaller.
Now you can clone this repository and install the necessary dependencies:

## Code Quality
```
git clone https://github.com/jmcnally17/instagram-challenge
npm install
```

For linting, you can use the `.rubocop.yml` in this repository (or your own!).
You'll need these gems:
Mongodb needs to be installed and started as well:

```ruby
group :development, :test do
gem 'rubocop', '1.20', require: false
gem 'rubocop-rails'
end
```
brew tap mongodb/brew
brew install mongodb-community@5.0
brew services start mongodb-community@5.0
```

## How To Use

To start using the app, start by running the server using:

```
npm start
```

Then, in your browser, enter `localhost:3000` in the address bar to visit the site. There, you can sign up, log in, log out and post image URLs that will be rendered on the posts page, which you can only visit when you are signed in.

## Testing

Jest was used for unit tests which applied to the models, while Cypress carried out integration tests to mimic user input in order to see the correct results were displayed on the pages. To run the unit tests, simply enter `jest` or `npm run test:unit` into the terminal. In order to run the integration tests, first the test server needs to be run by entering:

```
npm run start:test
```

Then the integration tests can be initiated by entering:

```
npm run test:integration
```

All tests can be ran together at once by simply entering `npm test` into the terminal *(remember the integration tests will need the test server to be runnning in order for them to pass)*.

## Improvements

Had I had more time, I would have liked to implement the following functionality:

You can also lint Javascript, CSS, and ERB — feel free to research this. These
will help you to train yourself to produce cleaner code — and will often alert
you to mistakes or mishaps!
1. Users are automatically logged in after registering.
2. Users cannot sign up with existing emails and passwords would be encrypted
3. Posts would be linked with the user who posted it via a foreign key.
4. Image files could be uploaded to the site instead of having to enter the URL for an existing photo on the internet.
5. Photos can be liked and commented on.
6. Posts can be deleted.
7. Bonus: add filters to posts.
76 changes: 76 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const session = require("express-session");
const methodOverride = require("method-override");


const homeRouter = require('./routes/home');
const postsRouter = require('./routes/posts');
const sessionsRouter = require('./routes/sessions');
const usersRouter = require('./routes/users');

const app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(methodOverride("_method"));

app.use(
session({
key: "user_sid",
secret: "super_secret",
resave: false,
saveUninitialized: false,
cookie: {
expires: 600000,
},
})
);

app.use((req, res, next) => {
if (req.cookies.user_sid && !req.session.user) {
res.clearCookie("user_sid");
}
next();
});

const sessionChecker = (req, res, next) => {
if (!req.session.user && !req.cookies.user_sid) {
res.redirect('/');
} else {
next();
}
};

app.use('/', homeRouter);
app.use('/posts', sessionChecker, postsRouter);
app.use('/sessions', sessionsRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;
101 changes: 101 additions & 0 deletions bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('instagram-challenge:server');
var http = require('http');
const mongoose = require('mongoose');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Connect to mongodb
*/

var mongoDbUrl = process.env.MONGODB_URI || "mongodb://127.0.0.1/fakesta";
mongoose.connect(mongoDbUrl, {
useNewUrlParser: true,
useUnifiedTopology: true,
});

var db = mongoose.connection;
db.on("error", console.error.bind(console, "MongoDB connection error:"));

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === "string" ? "Pipe " + port : "Port " + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port;
console.log("Now listening on " + bind);
debug('Listening on ' + bind);
}
7 changes: 7 additions & 0 deletions controllers/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const HomeController = {
Index: (req, res) => {
res.render('home/index', { title: 'Fakestagram' });
},
};

module.exports = HomeController;
24 changes: 24 additions & 0 deletions controllers/posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const Post = require('../models/post');

const PostsController = {
Index: (req, res) => {
Post.find((err, posts) => {
if (err) {
throw err;
}
res.render("posts/index", {posts: posts});
});
},

Create: (req, res) => {
const post = new Post(req.body);
post.save((err) => {
if (err) {
throw err;
}
res.status(200).redirect('/posts');
});
},
};

module.exports = PostsController;
Loading