Skip to content

Commit

Permalink
upload file
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenilk committed Jun 3, 2023
1 parent a03b1b1 commit 34b5116
Show file tree
Hide file tree
Showing 10 changed files with 665 additions and 48 deletions.
Binary file added App/Screenshot from 2023-06-02 19-11-21.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added App/mongodb-compass_1.37.0_amd64.deb
Binary file not shown.
45 changes: 44 additions & 1 deletion api.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,24 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", { value: true });
require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
const app = express();
const port = 3000 || process.env.PORT;
const port = 3000;
const cors = require("cors");
const multer = require("multer");
const path = require("path");
var bodyParser = require('body-parser');
mongoose.connect("mongodb+srv://Lenilk:[email protected]/Refrigurator");
const databased = mongoose.connection;
const InRefrigurator = require("./model/inRefriguratorModel");
const Want = require("./model/wantModel");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors());
var formidable = require('formidable');
var fs = require('fs');
databased.on('error', (error) => {
console.log(error);
});
Expand All @@ -44,6 +51,42 @@ app.get("/getWant", (req, res) => __awaiter(void 0, void 0, void 0, function* ()
res.status(500).json({ message: error.message });
}
}));
app.get("/version", (req, res) => __awaiter(void 0, void 0, void 0, function* () {
yield fs.readFile("appversion.txt", 'utf-8', (err, data) => __awaiter(void 0, void 0, void 0, function* () {
let json = yield JSON.stringify({ "version": data });
res.json(json);
console.log(data);
}));
}));
app.get("/updatePage", (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('<form action="updateApp" method="post" enctype="multipart/form-data">');
res.write('<input type="text" name="version" placeholder="version"><br>');
res.write('<input type="file" name="filetoupload"><br>');
res.write('<input type="submit">');
res.write('</form>');
return res.end();
});
app.post("/updateApp", (req, res) => {
var form = formidable({ multiples: true });
console.log(form);
form.parse(req, function (err, fields, files) {
var oldpath = files.filetoupload.filepath;
var version = fields["version"];
var newpath = path.join(__dirname + "/App/" + files.filetoupload.originalFilename);
fs.rename(oldpath, newpath, function (err) {
if (err)
throw err;
res.write('File uploaded and moved!');
res.end();
});
fs.writeFile('appversion.txt', String(version), function (err) {
if (err)
throw err;
console.log('Saved!');
});
});
});
app.post("/postRf", (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const data = InRefrigurator({
name: req.body.name,
Expand Down
122 changes: 79 additions & 43 deletions api.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { Response,Request } from "express";
import { Response, Request } from "express";
import { version } from "mongoose";
require("dotenv").config();
const express= require("express");
const express = require("express");
const mongoose = require("mongoose");
const app =express();
const port =3000;
const app = express();
const port = 3000;
const cors = require("cors")
const multer = require("multer")
const path = require("path")
var bodyParser = require('body-parser')
mongoose.connect("mongodb+srv://Lenilk:[email protected]/Refrigurator");
const databased = mongoose.connection
const InRefrigurator=require("./model/inRefriguratorModel");
const Want=require("./model/wantModel");
const InRefrigurator = require("./model/inRefriguratorModel");
const Want = require("./model/wantModel");
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))


databased.on('error', (error:Error) => {
app.use(cors())
var formidable = require('formidable');
var fs = require('fs');
databased.on('error', (error: Error) => {
console.log(error)
})

Expand All @@ -22,80 +27,111 @@ databased.once('connected', () => {
})


app.get("/getRf",async(req:Request,res:Response)=>{
try{
app.get("/getRf", async (req: Request, res: Response) => {
try {
const data = await InRefrigurator.find();
res.json(data)
}
catch(error:any){
res.status(500).json({message: error.message})
catch (error: any) {
res.status(500).json({ message: error.message })
}
})

app.get("/getWant",async(req:Request,res:Response)=>{
try{
app.get("/getWant", async (req: Request, res: Response) => {
try {
const data = await Want.find();
res.json(data)
}
catch(error:any){
res.status(500).json({message: error.message})
catch (error: any) {
res.status(500).json({ message: error.message })
}
})


app.post("/postRf", async(req:Request,res:Response)=>{
const data= InRefrigurator({
name:req.body.name,
Amount:req.body.Amount
app.get("/version",async(req: Request, res: Response)=>{
await fs.readFile("appversion.txt",'utf-8',async(err:Error,data:any)=>{
let json =await JSON.stringify({"version":data})
res.json(json)
})
})
app.get("/updatePage", (req: Request, res: Response) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('<form action="updateApp" method="post" enctype="multipart/form-data">');
res.write('<input type="text" name="version" placeholder="version"><br>');
res.write('<input type="file" name="filetoupload"><br>');
res.write('<input type="submit">');
res.write('</form>');
return res.end();
})
app.post("/updateApp", (req: Request, res: Response) => {
var form = formidable({multiples:true})
console.log(form)
form.parse(req, function (err: Error, fields: any, files: any) {
var oldpath = files.filetoupload.filepath;
var version = fields["version"];
var newpath = path.join(__dirname + "/App/" + files.filetoupload.originalFilename);
fs.rename(oldpath, newpath, function (err: Error) {
if (err) throw err;
res.write('File uploaded and moved!');
res.end();
});
fs.writeFile('appversion.txt', String(version), function (err:Error) {
if (err) throw err;
console.log('Saved!');
});
})
})
app.post("/postRf", async (req: Request, res: Response) => {
const data = InRefrigurator({
name: req.body.name,
Amount: req.body.Amount
})
try {
const dataToSave = await data.save();
const dataToSave = await data.save();
res.status(200).json(dataToSave)
}
catch (error:any) {
res.status(400).json({message: error.message})
catch (error: any) {
res.status(400).json({ message: error.message })
}
})

app.post("/postWant",async(req:Request,res:Response)=>{
const data= Want({
name:req.body.name,
Amount:req.body.Amount,
Comment:req.body.Comment
app.post("/postWant", async (req: Request, res: Response) => {
const data = Want({
name: req.body.name,
Amount: req.body.Amount,
Comment: req.body.Comment
})
try {
const dataToSave = await data.save();
const dataToSave = await data.save();
res.status(200).json(dataToSave)
}
catch (error:any) {
res.status(400).json({message: error.message})
catch (error: any) {
res.status(400).json({ message: error.message })
}
})


app.delete("/deleteRf/:id",async(req:Request,res:Response)=>{
app.delete("/deleteRf/:id", async (req: Request, res: Response) => {
try {
const id = req.params.id;
const data = await InRefrigurator.findByIdAndDelete(id)
res.send(`Document with ${data.name} has been deleted..`)
}
catch (error:any) {
catch (error: any) {
res.status(400).json({ message: error.message })
}
})

app.delete("/deleteWant/:id",async(req:Request,res:Response)=>{
app.delete("/deleteWant/:id", async (req: Request, res: Response) => {
try {
const id = req.params.id;
const data = await Want.findByIdAndDelete(id)
res.send(`Document with ${data.name} has been deleted..`)
}
catch (error:any) {
catch (error: any) {
res.status(400).json({ message: error.message })
}
})

app.patch('/updateRf/:id', async (req:Request, res:Response) => {
app.patch('/updateRf/:id', async (req: Request, res: Response) => {
try {
const id = req.params.id;
const updatedData = req.body;
Expand All @@ -107,12 +143,12 @@ app.patch('/updateRf/:id', async (req:Request, res:Response) => {

res.send(result)
}
catch (error:any) {
catch (error: any) {
res.status(400).json({ message: error.message })
}
})

app.patch('/updateWant/:id', async (req:Request, res:Response) => {
app.patch('/updateWant/:id', async (req: Request, res: Response) => {
try {
const id = req.params.id;
const updatedData = req.body;
Expand All @@ -124,11 +160,11 @@ app.patch('/updateWant/:id', async (req:Request, res:Response) => {

res.send(result)
}
catch (error:any) {
catch (error: any) {
res.status(400).json({ message: error.message })
}
})

app.listen(port,()=>{
app.listen(port, () => {
console.log(`start in ${port}`);
})
1 change: 1 addition & 0 deletions appversion.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fs
Loading

0 comments on commit 34b5116

Please sign in to comment.