-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
10 changed files
with
665 additions
and
48 deletions.
There are no files selected for viewing
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
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 |
---|---|---|
|
@@ -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); | ||
}); | ||
|
@@ -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, | ||
|
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 |
---|---|---|
@@ -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) | ||
}) | ||
|
||
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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}`); | ||
}) |
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 @@ | ||
fs |
Oops, something went wrong.