Created
July 21, 2019 00:03
-
-
Save diegograssato/23ca00b5ee301e3c52d2594f73a992fc to your computer and use it in GitHub Desktop.
node with express
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var path = require('path'); | |
// var http = require('http'); | |
var app = express(); | |
var path = require('path'); | |
var routes = require('./routes'); | |
var bodyParser = require('body-parser'); | |
app.set('views', './views_dir'); | |
app.set('view engine', 'pug'); | |
app.use(function(req, res, next) { | |
req.name = 'SON'; | |
console.log('I AM A CUSTOm MIDDLEWARE'); | |
next(); | |
}); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ | |
extended: false | |
})); | |
app.get('/', function(req, res) { | |
res.render('index', { | |
message: 'Hello world from express by ' + req.name | |
}); | |
// res.send('Hello world from express by ' + req.name); | |
}); | |
app.get('/world', function(req, res){ | |
res.send('world'); | |
}); | |
app.use('/hello', routes); | |
app.use('/public', express.static(path.join(__dirname, 'public'))); | |
// app.get('/swagger.json', function (req, res) { | |
// res.setHeader('Content-Type', 'application/json'); | |
// res.send(swaggerSpec); | |
// }); | |
const swaggerUi = require('swagger-ui-express'); | |
const swaggerSpec = require('./swagger-spec'); | |
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec)); | |
app.use(function(err, req, res, next) { | |
res.status(500) | |
.json({ | |
message: 'Something wrong happens' | |
}); | |
}); | |
// http.createServer(app).listen(3000, function() { | |
// console.log('Express started'); | |
// }); | |
app.listen(3000, function() { | |
console.log('Express started'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "my_express", | |
"version": "1.0.0", | |
"description": "Minha aplicação lindia", | |
"main": "app.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "Leonan <SON>", | |
"license": "ISC", | |
"dependencies": { | |
"body-parser": "^1.15.2", | |
"express": "^4.14.0", | |
"pug": "^2.0.0-beta6", | |
"swagger-jsdoc": "^3.3.0", | |
"swagger-ui-express": "^4.0.7" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var swaggerJSDoc = require('swagger-jsdoc'); | |
const { version, name, description } = require('./package.json'); | |
// swagger definition | |
var swaggerDefinition = { | |
info: { | |
title: 'Swagger API from: ' + name, | |
version: version, | |
description: description, | |
}, | |
host: 'localhost:3000', | |
basePath: '/', | |
}; | |
// options for the swagger docs | |
var options = { | |
// import swaggerDefinitions | |
swaggerDefinition: swaggerDefinition, | |
// path to the API docs | |
apis: ['*.js'], | |
}; | |
// initialize swagger-jsdoc | |
var swaggerSpec = swaggerJSDoc(options); | |
module.exports = swaggerSpec; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/Surnet/swagger-jsdoc/blob/master/docs/GETTING-STARTED.md