Skip to content

Commit a172757

Browse files
committed
Switch to es6 import/export
1 parent 8ff4a1e commit a172757

File tree

14 files changed

+605
-369
lines changed

14 files changed

+605
-369
lines changed

app.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
const bodyParser = require('body-parser');
2-
const express = require('express');
3-
const fs = require('fs');
4-
const yaml = require('js-yaml');
5-
const logger = require('morgan');
6-
const mongoose = require('mongoose');
7-
const path = require('path');
8-
const swaggerUi = require('swagger-ui-express');
9-
10-
const config = require('./config');
11-
const moviesApi = require('./routes/movies');
12-
const peopleApi = require('./routes/people');
13-
const adminRoutes = require('./routes/admin');
14-
const unrestRoutes = require('./unrest/routes');
1+
import bodyParser from 'body-parser';
2+
import express from 'express';
3+
import fs from 'fs';
4+
import yaml from 'js-yaml';
5+
import logger from 'morgan';
6+
import mongoose from 'mongoose';
7+
import path from 'path';
8+
import swaggerUi from 'swagger-ui-express';
9+
10+
import * as config from './config.js';
11+
import moviesApi from './routes/movies.js';
12+
import peopleApi from './routes/people.js';
13+
import adminRoutes from './routes/admin.js';
14+
import unrestRoutes from './unrest/routes.js';
1515

1616
// Connect to the database (can be overriden from environment)
1717
mongoose.connect(config.databaseUrl);
@@ -23,7 +23,7 @@ if (config.debug) {
2323
}
2424

2525
// View engine setup
26-
app.set('views', path.join(__dirname, 'views'));
26+
app.set('views', path.join(config.projectRoot, 'views'));
2727
app.set('view engine', 'pug');
2828

2929
// General middlewares
@@ -41,7 +41,7 @@ openApiDocument.servers[0].url = `${config.baseUrl}/api`;
4141
app.use('/swagger', swaggerUi.serve, swaggerUi.setup(openApiDocument));
4242

4343
// Serve the apiDoc documentation on /.
44-
app.use(express.static(path.join(__dirname, 'docs')));
44+
app.use(express.static(path.join(config.projectRoot, 'docs')));
4545

4646
// REST API routes
4747
app.use('/api/movies', moviesApi);
@@ -94,4 +94,4 @@ app.use(function (err, req, res, next) {
9494
res.render('error');
9595
});
9696

97-
module.exports = app;
97+
export default app;

bin/www renamed to bin/www.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#!/usr/bin/env node
22

3-
// Module dependencies
4-
const app = require('../app');
5-
const config = require('../config');
6-
const debug = require('debug')('demo:server');
7-
const http = require('http');
3+
import debugFactory from 'debug';
4+
import http from 'http';
5+
6+
import app from '../app.js';
7+
import * as config from '../config.js';
8+
9+
const debug = debugFactory('demo:server');
810

911
// Get port from environment and store in Express
1012
const port = normalizePort(config.port);
@@ -22,16 +24,16 @@ server.on('listening', onListening);
2224
* Normalize a port into a number, string, or false.
2325
*/
2426
function normalizePort(val) {
25-
const port = parseInt(val, 10);
27+
const parsedPort = parseInt(val, 10);
2628

27-
if (isNaN(port)) {
29+
if (isNaN(parsedPort)) {
2830
// named pipe
2931
return val;
3032
}
3133

32-
if (port >= 0) {
34+
if (parsedPort >= 0) {
3335
// port number
34-
return port;
36+
return parsedPort;
3537
}
3638

3739
return false;
@@ -52,11 +54,9 @@ function onError(error) {
5254
case 'EACCES':
5355
console.error(bind + ' requires elevated privileges');
5456
process.exit(1);
55-
break;
5657
case 'EADDRINUSE':
5758
console.error(bind + ' is already in use');
5859
process.exit(1);
59-
break;
6060
default:
6161
throw error;
6262
}

config.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
const env = process.env.NODE_ENV || 'development';
1+
import { fileURLToPath, URL } from 'url';
2+
3+
export const projectRoot = fileURLToPath(new URL('.', import.meta.url));
4+
5+
export const env = process.env.NODE_ENV || 'development';
26

37
let defaultDatabaseUrl = 'mongodb://localhost/comem-rest-demo';
48
if (env === 'test') {
59
defaultDatabaseUrl = `${defaultDatabaseUrl}-test`;
610
}
711

8-
exports.authToken = process.env.AUTH_TOKEN;
9-
exports.debug = !!process.env.DEBUG;
10-
exports.env = env;
11-
exports.port = process.env.PORT || '3000';
12-
exports.databaseUrl = process.env.DATABASE_URL || process.env.MONGODB_URI || defaultDatabaseUrl;
13-
exports.baseUrl = process.env.BASE_URL || `http://localhost:${exports.port}`;
12+
export const authToken = process.env.AUTH_TOKEN;
13+
14+
export const debug = !!process.env.DEBUG;
15+
16+
export const port = process.env.PORT || '3000';
17+
18+
export const databaseUrl =
19+
process.env.DATABASE_URL || process.env.MONGODB_URI || defaultDatabaseUrl;
20+
21+
export const baseUrl = process.env.BASE_URL || `http://localhost:${port}`;

models/movie.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const mongoose = require('mongoose');
1+
import mongoose from 'mongoose';
2+
23
const ObjectId = mongoose.Types.ObjectId;
34
const Schema = mongoose.Schema;
45

@@ -105,4 +106,4 @@ function transformJsonMovie(doc, json, options) {
105106
return json;
106107
}
107108

108-
module.exports = mongoose.model('Movie', movieSchema);
109+
export default mongoose.model('Movie', movieSchema);

models/person.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const mongoose = require('mongoose');
2-
const ObjectId = mongoose.Types.ObjectId;
1+
import mongoose from 'mongoose';
2+
33
const Schema = mongoose.Schema;
44

55
/**
@@ -68,4 +68,4 @@ function transformJsonPerson(doc, json, options) {
6868
return json;
6969
}
7070

71-
module.exports = mongoose.model('Person', personSchema);
71+
export default mongoose.model('Person', personSchema);

0 commit comments

Comments
 (0)