Skip to content

Commit cb26f0e

Browse files
committed
message
0 parents  commit cb26f0e

26 files changed

+25871
-0
lines changed

AWS_S3-1.png

31.9 KB
Loading

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
A simple boilerplate project to implement AWS S3 file upload functionality in a Node, React and Mongo app. Using Multer for uploading file.
2+
3+
To launch this project in the local machine
4+
5+
First start mongodb service with `sudo service mongod start` and then the following commands
6+
7+
- Run `npm install`
8+
- Run `npm run dev`
9+
- Run `npm start`
10+
11+
It will start the server at [http://localhost:3000/](http://localhost:3000/)
12+
13+
### Most importantly remember to replace AWS S3's bucket_name, AWSAccessKeyId and AWSSecretKey wth your own. I have kept those keys of mine in the .env file in the project root, and which ofcourse have been put in the gitignore file so not be make them public.
14+
15+
<img src="AWS_S3-1.png">
16+
17+
<img src="app-running-in-localhost.png">
18+
19+
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
20+
21+
[NOTE ONLY FOR ME - I HAVE TO ENTER ALL DETAILS IN .env FILE AGAIN AS I HAVE DELETED THE S3-BUCKET ]

app-running-in-localhost.png

19.4 KB
Loading

app.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require("dotenv").config();
2+
const express = require("express");
3+
const path = require("path");
4+
const bodyParser = require("body-parser");
5+
const cors = require("cors");
6+
const fileUploadRoutes = require("./routes/fileUploadRoutes");
7+
const app = express();
8+
const config = require("./config/config");
9+
10+
app.use(cors());
11+
12+
// Replace the above three lines for connecting mongodb with the below single line
13+
config.connectDB();
14+
15+
app.use(bodyParser.json());
16+
app.use(bodyParser.urlencoded({ extended: "false" }));
17+
18+
app.use(express.static(path.join(__dirname, "build")));
19+
20+
// make the '/api/document' browser url route to go to documentRoutes.js route file
21+
app.use("/api/document", fileUploadRoutes);
22+
23+
// catch 404 and forward to error handler
24+
app.use(function(req, res, next) {
25+
var err = new Error("Not Found");
26+
err.status = 404;
27+
next(err);
28+
});
29+
30+
// error handler
31+
app.use(function(err, req, res, next) {
32+
// set locals, only providing error in development
33+
res.locals.message = err.message;
34+
res.locals.error = req.app.get("env") === "development" ? err : {};
35+
36+
// render the error page
37+
res.status(err.status || 500);
38+
res.status(err.status || 500);
39+
res.render("error");
40+
});
41+
42+
module.exports = app;

bin/www

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env node
2+
3+
require('dotenv').config();
4+
var app = require('../app');
5+
var debug = require('debug')('mean-app:server');
6+
var http = require('http');
7+
8+
// Get port from environment and store in Express.
9+
var port = normalizePort(process.env.PORT || '8080');
10+
app.set('port', port);
11+
12+
13+
// Create HTTP server.
14+
var server = http.createServer(app);
15+
16+
// Listen on provided port, on all network interfaces.
17+
server.listen(port);
18+
server.on('error', onError);
19+
server.on('listening', onListening);
20+
21+
// Normalize a port into a number, string, or false.
22+
function normalizePort(val) {
23+
var port = parseInt(val, 10);
24+
25+
if (isNaN(port)) {
26+
// named pipe
27+
return val;
28+
}
29+
30+
if (port >= 0) {
31+
// port number
32+
return port;
33+
}
34+
return false;
35+
}
36+
37+
38+
// Event listener for HTTP server "error" event.
39+
function onError(error) {
40+
if (error.syscall !== 'listen') {
41+
throw error;
42+
}
43+
44+
var bind = typeof port === 'string'
45+
? 'Pipe ' + port
46+
: 'Port ' + port;
47+
48+
// handle specific listen errors with friendly messages
49+
switch (error.code) {
50+
case 'EACCES':
51+
console.error(bind + ' requires elevated privileges');
52+
process.exit(1);
53+
break;
54+
case 'EADDRINUSE':
55+
console.error(bind + ' is already in use');
56+
process.exit(1);
57+
break;
58+
default:
59+
throw error;
60+
}
61+
}
62+
63+
64+
// Event listener for HTTP server "listening" event.
65+
function onListening() {
66+
var addr = server.address();
67+
var bind = typeof addr === 'string'
68+
? 'pipe ' + addr
69+
: 'port ' + addr.port;
70+
debug('Listening on ' + bind);
71+
}

config/config.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict'
2+
3+
const mongoose = require('mongoose');
4+
5+
module.exports = {
6+
'database': process.env.MONGO_DB,
7+
8+
// connect function to create a mongoDB connection
9+
'connectDB' : function () {
10+
mongoose.connect(this.database)
11+
},
12+
}
13+
// on mongo connection open event print a console statement
14+
mongoose.connection.on('open', function () {
15+
console.log('Connected to Database (MongoDB) ');
16+
})

config/settings.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
'secret':'apauth'
3+
};

models/Document.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const mongoose = require("mongoose");
2+
const Schema = mongoose.Schema;
3+
4+
// library for handling autoincrement in mongoose
5+
// https://github.com/ramiel/mongoose-sequence
6+
const AutoIncrement = require("mongoose-sequence")(mongoose);
7+
8+
let documentSchema = new Schema(
9+
{
10+
document_id: { type: Number, default: 0 },
11+
description: { type: String },
12+
fileLink: { type: String }
13+
},
14+
{
15+
// createdAt,updatedAt fields are automatically added into records
16+
timestamps: true
17+
}
18+
);
19+
20+
documentSchema.plugin(AutoIncrement, { inc_field: "document_id" });
21+
22+
module.exports = mongoose.model("Document", documentSchema);
23+
24+
/* The mongoose-sequence creates a commodity collection named 'counters' which keeps track of the auto-incremental number. So during development to reset the go_id back to 1, I just have to drop the counter collection by running db.counters.drop() */

0 commit comments

Comments
 (0)