Skip to content

Commit

Permalink
ejs added to the api
Browse files Browse the repository at this point in the history
  • Loading branch information
BurhanRaja committed Aug 11, 2022
1 parent 4eee105 commit 11fa773
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
APP_BASE_URL='http://localhost:3000'
APP_BASE_URL='http://localhost:5000'
MONGODB_URI=
SMTP_HOST=
SMTP_POST=
Expand Down
12 changes: 3 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,14 @@ app.use(express.json())
app.use(cors())

// routes
app.get('/', (req, res) => {
res.render('home')
})
app.use('/api/files', require("./routes/files"))
app.use('/files', require("./routes/show"))
app.use('/file/download', require('./routes/download'))


// Docs
const swaggerUi = require('swagger-ui-express');
const swaggerJsdoc = require("swagger-jsdoc");
const options = require('./docs')

const specs = swaggerJsdoc(options);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs))


app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
36 changes: 4 additions & 32 deletions routes/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,6 @@ const sendEmail = require('../services/emailService')
require('dotenv').config()


/**
* @swagger
* /api/files:
* post:
* summary: Create a JSONPlaceholder user.
* requestBody:
* required: true
* content:
* multipart/form-data:
* schema:
* type: string
* format: base64
* properties:
* name:
* type: string
* example: myfile
* responses:
* 201:
* description: Created
* content:
* application/json:
* schema:
* type: object
* properties:
* filePath:
* type: string
* description: The user's name.
* example: Leanne Graham
*/


const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/')
Expand Down Expand Up @@ -73,7 +42,10 @@ router.post('/', (req, res) => {

const response = await fileUpload.save()

return res.send({ filePath: `${process.env.APP_BASE_URL}/files/${response.uuid}` })
return res.render('pathsToDownload', {
filePath: `${process.env.APP_BASE_URL}/files/${response.uuid}`,
uuid: response.uuid,
})
})
})

Expand Down
Binary file removed uploads/1659506843003-278568111.jpg
Binary file not shown.
Binary file removed uploads/1659517983803-227460010.jpg
Binary file not shown.
27 changes: 27 additions & 0 deletions views/home.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>

<body>
<section class="downloadSection">

<div class="upload">
<form method="post" action="/api/files" enctype="multipart/form-data">
<label for="myfile">myfile</label>
<input type="file" id="myfile" name="myfile" multiple>
<button>Upload</button>
</form>
</div>



</section>
</body>

</html>
80 changes: 80 additions & 0 deletions views/pathsToDownload.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>

<body>
<section class="downloadSection">

<section class="downloadSection">

<% if (locals.error) { %>
<h4>
<%= locals.error %>
</h4>
<% } else { %>
<div class="send-download">
<div class="downloadLink">
<a href="<%= filePath %>">
<%= filePath %>
</a>
</div>
<p>OR</p>
<div class="send-email">
<!-- <form>
<input type="text" value="<%= uuid %>" name="uuid" id="uuid">
<label for="emailFrom">Sender</label>
<input type="email" name="emailFrom" id="emailFrom">
<label for="emailTo">Receiver</label>
<input type="email" name="emailTo" id="emailTo">
<button onclick={handleSubmit} type="submit" id="send-btn">Submit</button>
</form> -->
</div>
</div>
<% } %>
</section>

</section>

<script>
const inputSenderEl = document.getElementById('emailFrom')
const inputReceiverEl = document.getElementById('emailTo')
const inputUuidEl = document.getElementById('uuid')
let data = {
uuid: inputUuidEl.value,
emailFrom: inputSenderEl.value,
emailTo: inputReceiverEl.value
}
data = JSON.stringify(data)
const handleSubmit = async () => {
const response = await fetch('http://localhost/api/files/send', {
method:'POST',
headers:{
'Content-type':"application/json",
},
body: data
})
}
console.log(data)
</script>
</body>

</html>

0 comments on commit 11fa773

Please sign in to comment.