Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a251768
Showing
32 changed files
with
17,404 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"presets": [ | ||
[ | ||
"@babel/preset-env", | ||
{ | ||
"targets": { | ||
"node": "current" | ||
} | ||
} | ||
] | ||
], | ||
"plugins": [ | ||
"@babel/plugin-proposal-class-properties" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules/ | ||
uploads/ |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"name": "api", | ||
"version": "1.0.0", | ||
"description": "Server api for photato", | ||
"main": "dist", | ||
"author": "Foysal Ahamed", | ||
"license": "ISC", | ||
"entry": "src/index.js", | ||
"scripts": { | ||
"dev": "NODE_ENV=development nodemon src/index.js --exec babel-node", | ||
"start": "node dist", | ||
"build": "./node_modules/.bin/babel src --out-dir dist --copy-files", | ||
"prestart": "npm run -s build" | ||
}, | ||
"eslintConfig": { | ||
"extends": "eslint:recommended", | ||
"parserOptions": { | ||
"ecmaVersion": 7, | ||
"sourceType": "module" | ||
}, | ||
"env": { | ||
"node": true | ||
}, | ||
"rules": { | ||
"no-console": 0, | ||
"no-unused-vars": 1 | ||
} | ||
}, | ||
"dependencies": { | ||
"cors": "^2.8.4", | ||
"express": "^4.13.3", | ||
"multer": "^1.4.2", | ||
"mysql2": "^1.6.1", | ||
"sequelize": "^5.18.4" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.1.2", | ||
"@babel/core": "^7.1.2", | ||
"@babel/node": "^7.0.0", | ||
"@babel/plugin-proposal-class-properties": "^7.1.0", | ||
"@babel/preset-env": "^7.1.0", | ||
"eslint": "^3.1.1", | ||
"eslint-config-airbnb": "^17.1.0", | ||
"eslint-plugin-jsx-a11y": "^6.2.1", | ||
"nodemon": "^1.9.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import http from 'http'; | ||
import cors from 'cors'; | ||
import multer from 'multer'; | ||
import express from 'express'; | ||
import { resolve, join } from 'path'; | ||
import { Sequelize } from 'sequelize'; | ||
|
||
import PhotoModel from './photo.model'; | ||
|
||
const config = { | ||
port: 3001, | ||
uploadDir: `${resolve(__dirname, '..')}/uploads/`, | ||
database: { | ||
username: "root", | ||
password: "admin", | ||
host: "localhost", | ||
port: "3306", | ||
dialect: "mysql", | ||
database: "photato", | ||
} | ||
}; | ||
|
||
let app = express(); | ||
app.server = http.createServer(app); | ||
|
||
// 3rd party middlewares | ||
app.use(cors({})); | ||
|
||
// connect to db | ||
const database = new Sequelize(config.database); | ||
|
||
// initialize models | ||
const Photo = PhotoModel.init(database); | ||
|
||
// setup multer | ||
const uploadMiddleware = multer({ | ||
dest: config.uploadDir, | ||
fileFilter: function (req, file, cb) { | ||
if (!file.originalname.match(/\.(jpg|jpeg|png|gif)$/)) { | ||
return cb(new Error('Only image files are allowed!')); | ||
} | ||
cb(null, true); | ||
}, | ||
}).single('photo'); | ||
|
||
database.sync().then(() => { | ||
app.get('/', (req, res) => { | ||
res.json({app: 'photato'}); | ||
}); | ||
|
||
app.get("/photo/:filename", (req, res) => { | ||
res.sendFile(join(config.uploadDir, `/${req.params.filename}`)); | ||
}); | ||
|
||
app.get('/photo', async (req, res) => { | ||
const photos = await Photo.findAndCountAll(); | ||
res.json({success: true, photos}); | ||
}); | ||
|
||
app.post('/photo', uploadMiddleware, async (req, res) => { | ||
try { | ||
const photo = await Photo.create(req.file); | ||
res.json({success: true, photo}); | ||
} catch (err) { | ||
res.status(400).json({success: false, message: err.message}); | ||
} | ||
}); | ||
|
||
app.server.listen(process.env.PORT || config.port, () => { | ||
console.log(`Started on port ${app.server.address().port}`); | ||
}); | ||
}); | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Model, DataTypes } from 'sequelize'; | ||
|
||
const PhotoSchema = { | ||
originalname: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
mimetype: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
size: { | ||
type: DataTypes.INTEGER, | ||
allowNull: false, | ||
}, | ||
filename: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
path: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
}; | ||
|
||
class Photo extends Model { | ||
static init (sequelize) { | ||
return super.init(PhotoSchema, { sequelize }); | ||
} | ||
}; | ||
|
||
export default Photo; |
Oops, something went wrong.