init
This commit is contained in:
58
src/index.ts
Normal file
58
src/index.ts
Normal file
@ -0,0 +1,58 @@
|
||||
import cfenv from 'cfenv';
|
||||
import express from 'express';
|
||||
import mongoose from 'mongoose';
|
||||
import swagger from 'swagger-ui-express';
|
||||
import yaml from 'yamljs';
|
||||
|
||||
|
||||
// tell if server is running in debug or production environment
|
||||
console.log(process.env.NODE_ENV === 'production' ? '===== PRODUCTION =====' : '===== DEVELOPMENT =====');
|
||||
|
||||
|
||||
// get mongodb address from server, otherwise set to localhost
|
||||
let connectionString: string = "";
|
||||
if(process.env.NODE_ENV === 'production') {
|
||||
let services = cfenv.getAppEnv().getServices();
|
||||
for (let service in services) {
|
||||
if(services[service].tags.indexOf("mongodb") >= 0) {
|
||||
connectionString = services[service]["credentials"].uri;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
connectionString = 'mongodb://localhost/dfopdb';
|
||||
}
|
||||
mongoose.connect(connectionString, {useNewUrlParser: true, useUnifiedTopology: true});
|
||||
|
||||
// connect to mongodb
|
||||
let db = mongoose.connection;
|
||||
db.on('error', console.error.bind(console, 'connection error:'));
|
||||
db.once('open', () => {
|
||||
console.log(`Connected to ${connectionString}`);
|
||||
});
|
||||
|
||||
|
||||
// create Express app
|
||||
const app = express();
|
||||
app.disable('x-powered-by');
|
||||
|
||||
// get port from environment, defaults to 3000
|
||||
const port = process.env.PORT || 3000;
|
||||
|
||||
// require routes
|
||||
app.use('/', require('./routes/root'));
|
||||
|
||||
// Swagger UI
|
||||
app.use('/api', swagger.serve, swagger.setup(
|
||||
yaml.load('./oas.yml'),
|
||||
{
|
||||
defaultModelsExpandDepth: -1,
|
||||
customCss: '.swagger-ui .topbar { display: none }'
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// hook up server to port
|
||||
app.listen(port, () => {
|
||||
console.log(`Listening on http;//localhost:${port}`);
|
||||
});
|
19
src/routes/root.spec.ts
Normal file
19
src/routes/root.spec.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import supertest from 'supertest';
|
||||
import should from 'should/as-function';
|
||||
|
||||
|
||||
let server = supertest.agent('http://localhost:3000');
|
||||
|
||||
describe('Testing /', () => {
|
||||
it('returns the message object', done => {
|
||||
server
|
||||
.get('/')
|
||||
.expect('Content-type', /json/)
|
||||
.expect(200)
|
||||
.end(function(err, res) {
|
||||
should(res.statusCode).equal(200);
|
||||
should(res.body).be.eql({message: 'API server up and running!'});
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
9
src/routes/root.ts
Normal file
9
src/routes/root.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import express from 'express';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', (req, res) => {
|
||||
res.json({message: 'API server up and running!'});
|
||||
});
|
||||
|
||||
module.exports = router;
|
Reference in New Issue
Block a user