-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello-world.js
More file actions
51 lines (44 loc) · 1.63 KB
/
hello-world.js
File metadata and controls
51 lines (44 loc) · 1.63 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
const express = require('express');
require('dotenv').config()
const port = process.env.PORT || 2500;
const app = express();
const path = require('path');
const apiResponse = require("./Service/apiResponse");
const routes = require("./Service/routes");
const cors = require('cors');
//applying cors only on localhost
const allowedDomains = ['http://localhost:2500', 'http://localhost:3000', 'http://www.helloworld.social/', 'https://www.helloworld.social/'];
app.use(cors({
origin: function (origin, callback) {
// bypass the requests with no origin (like curl requests, mobile apps, etc )
if (!origin) return callback(null, true);
if (allowedDomains.indexOf(origin) === -1) {
const msg = `This site ${origin} does not have an access. Only specific domains are allowed to access it.`;
return callback(new Error(msg), false);
}
return callback(null, true);
}
}));
// routing APIs
app.use('/api', routes);
//serving webpage files
app.use(express.json());
app.use(express.static('Client/build'));
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, '/Client/build/', 'index.html'), function (err) {
if (err) {
apiResponse.notFoundResponse(res, "404 Page not found")
}
});
});
// throw 404 if URL not found
app.all("*", function (req, res) {
return apiResponse.notFoundResponse(res, "404 Page not found");
});
// start server listen and port number
app.listen(port, () => {
console.log('Server started! At http://localhost:' + port);
});
// init Moralis server
const { initMoralis } = require("./Service/moralis_init");
initMoralis();