-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
95 lines (76 loc) · 2.24 KB
/
Copy pathapp.js
File metadata and controls
95 lines (76 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const express = require('express')
const mongoose = require('mongoose')
require('dotenv').config()
//const CourseModel = require('./models/channel.js')
const path = require('path');
const fs = require('fs');
//const dbURL = process.env.DB_URL
const dbURL = "mongodb+srv://dxgd33:Password_1234@cluster0.ugkctfh.mongodb.net/"
//express app
const app = express()
app.use(express.json());
app.use(express.static(path.join(__dirname, 'client')));
//connect to db on mongodb atlas
mongoose
.connect(dbURL, {
useNewUrlParser: true,
})
.then(() => {
console.info('Connected to the database');
})
.catch((error) => {
console.log(error)
})
/*
app.get("/", (req, res) => {
res.send("Hello, world!");
});
const newCourse = new CourseModel({
id: 1,
title: 'title',
description: 'description',
link: 'hello'
});
newCourse.save()
.then(() => console.log('Course saved'))
.catch((error) => console.log(error));
app.get('/courseSearch', async (req, res) => {
if (!req.query.q) {
return res.status(400).send('Missing query parameter: q');
}
const query = req.query.q.toLowerCase();
try {
const courses = await CourseModel.find();
const results = courses.filter(
(course) => course.title.toLowerCase().includes(query)
|| course.link.toLowerCase().includes(query)
);
const formattedResults = results.map((course) => ({
title: course.title,
link: course.link,
}));
res.setHeader('Content-Type', 'application/json');
res.status(200).json(formattedResults);
} catch (err) {
console.error(err);
res.status(500).send('An error occurred while searching.');
}
});
*/
module.exports = app;
require("./userDetails")
const User = mongoose.model("userDetail");
app.post("/registration", async(req,res) =>{
const {username,name,email} = req.body;
try {
await User.create({
username,
uname:name,
uemail:email,
});
res.send({status:"Ok"});
}
catch (error){
res.send({status:"error"});
}
})