Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //geo lotto
- //
- ////
- ////
- ////
- ////
- ////
- //
- const express = require("express");
- const mysql = require("mysql");
- const app = express();
- function connectToDB() {
- const con = mysql.createConnection(
- process.env["DATABASE"] ||
- "mysql://uc5pedch5k37uuom:q491vkdlzk275756@gk90usy5ik2otcvi.cbetxkdyhwsb.us-east-1.rds.amazonaws.com:3306/d9g1kaehhpss06yw"
- );
- con.connect(err => {
- //if (err) throw err;
- console.log("Succesfully connected to database");
- });
- return con;
- }
- app.use((req, res, next) => {
- res.append("Access-Control-Allow-Origin", ["*"]);
- res.append("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
- res.append("Access-Control-Allow-Headers", "Content-Type");
- next();
- });
- app.get("/", (req, res) => {});
- app.get("/order/:id", (req, res) => {
- //nazwa, telefon, adres
- const id = req.params.id;
- const con = connectToDB();
- const sql = /*sql*/ `
- SELECT distinct r.name, r.phone as number, a.city, a.street, a.nr from restaurants r, adres a, dish d where d.id_restaurant = r.id_restaurant and r.id_adres = a.id_adres and d.id_dish = ?
- `;
- con.query(sql, [id], (err, result) => {
- if (err) throw err;
- if (result.length == 0) {
- res.status(404);
- res.send("");
- return;
- }
- res.send(result[0]);
- });
- });
- app.get("/dish/:id", (req, res) => {
- const id = req.params.id;
- const con = connectToDB();
- const sql = /*sql*/ `
- SELECT d.name, d.description, d.price , r.id_restaurant as rid, r.name as rname, a.city, a.street, a.nr, a.longitude, a.latitude, r.phone from dish d, restaurants r, adres a
- where r.id_restaurant = d.id_restaurant and r.id_adres = a.id_adres and d.id_dish = ?
- `;
- con.query(sql, [id], (err, result) => {
- if (err) throw err;
- if (result.length == 0) {
- con.end();
- res.status(404);
- res.send("");
- return;
- }
- res.header("Content-Type", "application/json");
- const dish = result[0];
- const sql = /*sql*/ `
- SELECT url from imagesdish where id_dish = ?
- `;
- con.query(sql, [id], (err, result) => {
- let retresult = [];
- for (const u of result) {
- retresult.push({
- name: dish.name,
- description: dish.description,
- price: dish.price,
- restaurant: {
- name: dish.rname,
- adres: {
- city: dish.city,
- street: dish.street,
- nr: dish.nr,
- longitude: dish.longitude,
- latitude: dish.latitude
- }
- },
- illustration: u.url
- });
- }
- const sql = /*sql*/ `
- SELECT url from imagesrestaurant where id_restaurant = ?
- `;
- con.query(sql, [dish.rid], (err, result) => {
- for (const u of result) {
- retresult.push({
- name: dish.name,
- description: dish.description,
- price: dish.price,
- restaurant: {
- name: dish.rname,
- adres: {
- city: dish.city,
- street: dish.street,
- nr: dish.nr,
- longitude: dish.longitude,
- latitude: dish.latitude
- }
- },
- illustration: u.url
- });
- }
- res.send(retresult);
- });
- con.end();
- });
- });
- });
- app.get("/dishes/:long/:lat", (req, res) => {
- const con = connectToDB();
- const sql = /*sql*/ `
- SELECT distinct d.id_dish as id, d.name as name, d.price as price, r.name as restaurantName, di.url as img FROM dish d, imagesdish di, restaurants r WHERE di.id_dish = d.id_dish and r.id_restaurant = d.id_restaurant
- `;
- res.header("Content-Type", "application/json");
- con.query(sql, (err, result) => {
- res.send(result);
- });
- con.end();
- });
- const port = process.env["PORT"] || 5000;
- app.listen(port, err => {
- if (err) throw err;
- console.log(`listening on port ${port}...`);
- });
- //lets code
- //
- ////
- ////
- ////
- ////
- ////
- ////
- ////
- ////
- ////
- ////
- ////
- ////
- ////
- //
- const express = require("express");
- const mysql = require("mysql");
- const cookieParser = require("cookie-parser");
- const session = require("express-session");
- const md5 = require("md5");
- const Joi = require("joi");
- const app = express();
- function connectToDB() {
- let conn = mysql.createConnection({
- host: "db4free.net",
- user: "geolotto",
- password: "geolotto",
- database: "geolotto"
- });
- conn.connect(err => {
- if (err) throw err;
- console.log("Conneceted to database");
- });
- return conn;
- }
- app.use(express.json());
- app.use((req, res, next) => {
- res.append("Access-Control-Allow-Origin", ["*"]);
- res.append("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
- res.append("Access-Control-Allow-Headers", "Content-Type");
- next();
- });
- app.use(cookieParser());
- app.use(
- session({
- path: "/",
- httpOnly: true,
- secure: false,
- maxAge: null,
- secret: "cookie_secret",
- name: "cookie_name",
- resave: true,
- saveUninitialized: true
- })
- );
- app.post("/login", (req, res) => {
- let schema = Joi.object().keys({
- login: Joi.required(),
- password: Joi.required()
- });
- Joi.validate(req.body, schema);
- let { login, password } = req.body;
- password=md5(md5(password));
- let conn = connectToDB();
- let sql = /*sql*/ `
- SELECT login, password from users where login = ? and password = ?
- `;
- conn.query(sql, [login, password], (error, result) => {
- if (error) throw error;
- if (result.length > 0) {
- if (!req.session.login) req.session.login = req.body.login;
- if (!req.session.password)
- req.session.password = password;
- res.status(200).send("sthsth123");
- }
- else res.status(500).send("fail");
- });
- });
- app.post("/register", (req, res) => {
- req.header("Content-Type", "application/json");
- res.header("Content-Type", "application/json");
- console.log(req.body);
- let schema = Joi.object().keys({
- login: Joi.string()
- .min(3)
- .max(49)
- .required(),
- password: Joi.string()
- .min(8)
- .max(49)
- .required(),
- email: Joi.string().email().required(),
- name: Joi.string()
- .min(3)
- .max(49)
- .required(),
- surname: Joi.string()
- .min(3)
- .max(49)
- .required()
- });
- Joi.validate(req.body, schema, (err, res) => {
- if (err) {
- res.status(500).send({text:"texthaha"});
- throw err;
- }
- });
- const { login, email } = req.body;
- let conn = connectToDB();
- let sql = /*sql*/ `
- SELECT login, email from users where login = ? or email = ?
- `;
- conn.query(sql, [login, email], (error, result) => {
- if (error) throw error;
- if (result.length > 0) {
- res.status(505).send("CHUJOWO");
- } else {
- req.body.password = md5(md5(req.body.password));
- let sql = /*sql*/ `
- INSERT INTO users SET ?
- `;
- conn.query(sql, req.body, (err, result) => {
- if (err) throw err;
- res.status(200).send("CHUJ");
- });
- }
- });
- });
- const PORT = process.env.PORT || 5000;
- app.listen(PORT, err => {
- if (err) throw err;
- console.log(`listening on port ${PORT}`);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement