1
1
// Example express application adding the parse-server module to expose Parse
2
2
// compatible API routes.
3
3
4
- const express = require ( 'express' ) ;
5
- const ParseServer = require ( 'parse-server' ) . ParseServer ;
6
- const path = require ( 'path' ) ;
7
- const args = process . argv || [ ] ;
8
- const test = args . some ( arg => arg . includes ( 'jasmine' ) ) ;
9
-
10
- const databaseUri = process . env . DATABASE_URI || process . env . MONGODB_URI ;
11
-
12
- if ( ! databaseUri ) {
13
- console . log ( 'DATABASE_URI not specified, falling back to localhost.' ) ;
14
- }
15
- const config = {
16
- databaseURI : databaseUri || 'mongodb://localhost:27017/dev' ,
4
+ import express from 'express' ;
5
+ import { ParseServer } from 'parse-server' ;
6
+ import path from 'path' ;
7
+ const __dirname = path . resolve ( ) ;
8
+ import http from 'http' ;
9
+
10
+ export const config = {
11
+ databaseURI :
12
+ process . env . DATABASE_URI || process . env . MONGODB_URI || 'mongodb://localhost:27017/dev' ,
17
13
cloud : process . env . CLOUD_CODE_MAIN || __dirname + '/cloud/main.js' ,
18
14
appId : process . env . APP_ID || 'myAppId' ,
19
15
masterKey : process . env . MASTER_KEY || '' , //Add your master key here. Keep it secret!
@@ -26,16 +22,17 @@ const config = {
26
22
// If you wish you require them, you can set them as options in the initialization above:
27
23
// javascriptKey, restAPIKey, dotNetKey, clientKey
28
24
29
- const app = express ( ) ;
25
+ export const app = express ( ) ;
30
26
31
27
// Serve static assets from the /public folder
32
28
app . use ( '/public' , express . static ( path . join ( __dirname , '/public' ) ) ) ;
33
29
34
30
// Serve the Parse API on the /parse URL prefix
35
- const mountPath = process . env . PARSE_MOUNT || '/parse' ;
36
- if ( ! test ) {
37
- const api = new ParseServer ( config ) ;
38
- app . use ( mountPath , api ) ;
31
+ if ( ! process . env . TESTING ) {
32
+ const mountPath = process . env . PARSE_MOUNT || '/parse' ;
33
+ const server = new ParseServer ( config ) ;
34
+ await server . start ( ) ;
35
+ app . use ( mountPath , server . app ) ;
39
36
}
40
37
41
38
// Parse Server plays nicely with the rest of your web routes
@@ -49,17 +46,12 @@ app.get('/test', function (req, res) {
49
46
res . sendFile ( path . join ( __dirname , '/public/test.html' ) ) ;
50
47
} ) ;
51
48
52
- const port = process . env . PORT || 1337 ;
53
- if ( ! test ) {
54
- const httpServer = require ( ' http' ) . createServer ( app ) ;
49
+ if ( ! process . env . TESTING ) {
50
+ const port = process . env . PORT || 1337 ;
51
+ const httpServer = http . createServer ( app ) ;
55
52
httpServer . listen ( port , function ( ) {
56
53
console . log ( 'parse-server-example running on port ' + port + '.' ) ;
57
54
} ) ;
58
55
// This will enable the Live Query real-time server
59
- ParseServer . createLiveQueryServer ( httpServer ) ;
56
+ await ParseServer . createLiveQueryServer ( httpServer ) ;
60
57
}
61
-
62
- module . exports = {
63
- app,
64
- config,
65
- } ;
0 commit comments