Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic backend setup #39

Merged
merged 9 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 4 additions & 0 deletions backend/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PORT=3001
MONGO_URL=mongodb://localhost:27017/
DB_NAME=shopify

1 change: 0 additions & 1 deletion backend/app.js

This file was deleted.

26 changes: 26 additions & 0 deletions backend/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { MongoClient } = require('mongodb');
require('dotenv').config();

const url = process.env.MONGO_URL || 'mongodb://localhost:27017/';
const dbName = process.env.DB_NAME || 'shopify';

let db = null;

async function connectToMongoDB() {
if (db) return db;

const client = new MongoClient(url, { useNewUrlParser: true, useUnifiedTopology: true });

try {
await client.connect();
console.log('Connected successfully to MongoDB server');
db = client.db(dbName);
return db;
} catch (err) {
console.error('An error occurred while connecting to MongoDB:', err);
throw err;
}
}

module.exports = { connectToMongoDB };

59 changes: 59 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
const FormDataModel = require('./models/FormData.js');


const app = express();
app.use(express.json());
app.use(cors());



mongoose.connect('mongodb://127.0.0.1:27017/shopify')
.then(() => console.log('MongoDB connected'))
.catch(err => console.log('MongoDB connection error:', err));

app.post('/register', async (req, res) => {
const { email, password } = req.body;

try {
const user = await FormDataModel.findOne({ email });

if (user) {
res.json("Already registered");
} else {
const newUser = new FormDataModel(req.body);
await newUser.save();
res.json(newUser);
}
} catch (err) {
res.status(500).json(err);
}
});

app.post('/login', async (req, res) => {
const { email, password } = req.body;

try {
const user = await FormDataModel.findOne({ email });

if (user) {
if (user.password === password) {
res.json("Success");
} else {
res.json("Wrong password");
}
} else {
res.json("No records found!");
}
} catch (err) {
res.status(500).json(err);
}
});



app.listen(3001, () => {
console.log("Server listening on http://127.0.0.1:3001");
});
11 changes: 11 additions & 0 deletions backend/models/FormData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const mongoose = require('mongoose');

const FormDataSchema = new mongoose.Schema({
name : String,
email: String,
password: String
})

const FormDataModel = mongoose.model('log_reg_form', FormDataSchema);

module.exports = FormDataModel;
16 changes: 16 additions & 0 deletions backend/node_modules/.bin/color-support

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions backend/node_modules/.bin/color-support.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions backend/node_modules/.bin/color-support.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions backend/node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions backend/node_modules/.bin/mime.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions backend/node_modules/.bin/mime.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions backend/node_modules/.bin/mkdirp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions backend/node_modules/.bin/mkdirp.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions backend/node_modules/.bin/mkdirp.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions backend/node_modules/.bin/node-pre-gyp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions backend/node_modules/.bin/node-pre-gyp.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions backend/node_modules/.bin/node-pre-gyp.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions backend/node_modules/.bin/nodemon

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions backend/node_modules/.bin/nodemon.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading