-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
167 lines (147 loc) · 4.35 KB
/
server.js
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//importing stuff
import express from 'express'
import mongoose from 'mongoose'
import cors from 'cors'
import multer from 'multer'
// import GridFsStorage from 'multer-gridfs-storage'
// import Grid from 'gridfs-stream'
import bodyParser from 'body-parser'
import path from 'path'
import Pusher from 'pusher'
import mongoProjects from './mongoProjects.js'
import mongoUpcoming from './mongoUpcoming.js'
import mongoFavourite from './mongoFavourite.js'
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Grid.mongo = mongoose.mongo
// require("dotenv").config({path:"./config.env"});
//app config
const app = express()
const port = process.env.PORT || 9000
//middlewares
app.use(bodyParser.json());
app.use(cors())
// app.use('/',express.static("./nft_web/build"));
//db config
const mongoURI = process.env.DATABASE_URI
const conn = mongoose.createConnection(mongoURI, {
});
const connection = mongoose.connect(mongoURI)
app.get('/retrieve/nftcollections', (req, res) => {
const itemModel = new mongoose.Schema({
name: String,
token_id: String,
image_url: String,
opensea_url: String,
curr_price: String,
symbol: String,
rank: Number,
total_score: Number,
listing_time: Number,
last_price: String,
last_sale_symbol: String
}, { collection: req.query.name })
let mongoItems
try {
mongoItems = mongoose.model(req.query.name)
} catch (error) {
mongoItems = mongoose.model(req.query.name, itemModel)
}
// const mongoItems = mongoose.model(req.query.name, itemMdel)
console.log(req.query.name);
try {
mongoItems.find().sort({ total_score: -1 }).exec(function (err, data) {
res.status(200).send(data);
})
} catch (error) {
res.status(500).send(error);
};
})
app.get('/retrieve/firstpage', (req, res) => {
const itemModel = new mongoose.Schema({
name: String,
token_id: String,
image_url: String,
opensea_url: String,
curr_price: String,
symbol: String,
rank: Number,
total_score: Number,
listing_time: Number,
last_price: String,
last_sale_symbol: String
}, { collection: req.query.name })
let mongoItems
try {
mongoItems = mongoose.model(req.query.name)
} catch (error) {
mongoItems = mongoose.model(req.query.name, itemModel)
}
console.log(req.query.name);
try {
mongoItems.find().sort({ total_score: -1 }).limit(40).exec(function (err, data) {
res.status(200).send(data)
})
} catch (error) {
res.status(500).send(error);
};
})
// app.get('/project_name', (req,res)=>{
// mongoose.connection.db.listCollections().toArray(function (err, names) {
// if (err) {
// console.log(err);
// } else {
// console.log(names);
// }
// res.status(200).send(names)
// mongoose.connection.close();
// });
// })
app.post('/upload/favourite', (req, res) => {
const dbFav = req.body
mongoFavourite.create(dbFav, (err, data) => {
if (err) {
res.status(500).send(err)
} else {
res.status(201).send(data)
}
})
})
app.get('/retrieve/nftfavourite', (req, res) => {
console.log(req.query.uid);
try {
mongoFavourite.find().exec(function (err, data) {
res.status(200).send(data);
})
} catch (error) {
res.status(500).send(error);
};
})
app.get('/retrieve/projects', (req, res) => {
try {
mongoProjects.find().exec(function (err, data) {
res.status(200).send(data);
})
} catch (error) {
res.status(500).send(error);
};
})
app.get('/retrieve/upcoming', (req, res) => {
try {
mongoUpcoming.find().sort({ date: 1 }).exec(function (err, data) {
res.status(200).send(data);
})
} catch (error) {
res.status(500).send(error);
};
})
if (process.env.NODE_ENV === "production"){
app.use(express.static("nft_web/build"));
app.get("*", (req,res)=>{
res.sendFile(path.resolve(__dirname,'nft_web','build','index.html'));
})
}
//listener
app.listen(port, () => console.log(`listening on localhost:${port}`))