Nodejs swagger integration - Express Router - node.js

I am aware of configuring swagger to my NodeJs project using JSON / YAML. Is there any way to automate the documentation like swagger doing for spring webservices.
Note: I am using Express Router to create endpoints.

Yes, the package swagger-autogen performs the automatic construction of the Swagger documentation.
First, install the package with: npm i swagger-autogen
If you have a endpoint/route file like this:
routes.js
const express = require('express')
const ToolsController = require('./controllers/ToolsController')
const router = express.Router()
const apiV1 = require('./controllers/ApiRoute1')
router.use('/v1', apiV1)
module.exports = router
Just declare a file called swagger.js, for example, with this:
swagger.js
const swaggerAutogen = require('swagger-autogen')()
swaggerAutogen('./swagger-output.json', ['./routes.js']);
In the package.json, add a script such as:
"scripts": {
"swagger-autogen": "node ./swagger.js"
}
And run: npm run swagger-autogen
The Swagger documentation will be in the swagger-output.json.
To add descriptions, tags, summary, and so on, access the https://www.npmjs.com/package/swagger-autogen#endpoints
Examples: Links to projects that cover the simplest use of this module as well as the most complete use. See the links below:
Example using Router
Example without Router

yes, you could use swagger-ui-express or other npm libraries =)

You can use jsdoc. For this you need to install npm packages
npm i swagger-jsdoc
npm I swagger-ui-express
Please refer
node js swagger with express server

Related

Node doesn't find local packages

I'm learning node. I've initialized a project with npm. I installed express, and everything looks alright.
npm list outputs this:
package.json looks like this:
And the project is structured like this:
with index.js just being the following:
const expressDep = require('express');
const app = express();
But when I try to execute index.js, node outputs the following:
What am I doing wrong ?
To my understanding, node just doesn't see any definition of express, but npm tells me everything is installed correctly. What's going on ?
How can the compiler know what is this express()?
Require is a built-in function to include external modules that exist in separate files.
If you want to use this app function you must use the variable you assigned the module. Like this:
const express = require("express");
const app = express();
or
const expressDep = require("express");
const app = expressDep();

SwaggerUI + NodeJs (Meteor)

I have a meteor app with a REST API by https://atmospherejs.com/simple/json-routes.
Now I want to document my API with SwaggerUI. I already used SwaggerUI in other projects and know you have to create a yaml or json sepc file, which then is being displayed by SwaggerUI.
So now I have discovered there are some existing swagger-ui packages for npm:
https://www.npmjs.com/package/swagger-ui
https://www.npmjs.com/package/swagger-ui-dist
which I installed by meteor npm install swagger-ui-dist --save-dev
But I have no clue what to do next or how to use them.
I am sure it must be something as simple as:
const swaggerui = require('swagger-ui');
swaggerui.specPath(pathToYaml);
swaggerui.url('/api-docs');
Can anyone help me with the first steps?

Messenger bot using Botkit on AWS: "bodyParser is not defined" error

I set up a Bitnami Node.JS instance on AWS. Installed default Botkit. Updated dependencies. Set up a basic Facebook page/app with Messenger integration. Attempted to run the default Messenger bot example (Facebook_bot.js).
Running the bot gets the following error on load:
ReferenceError: bodyParser is not defined
Since this is all out of the box and using the example Messenger bot, is this an issue with BotKit source? Or have I somehow managed to mess up the minimal setup involved here?
this can mean 1 of 3 things
the node.js file usually named index.js does not have var bodyParser = require('body-parser') less likely
you do not have body-parser installed from npm to do so in command line run npm i body-parser --save
if this is a remote solution and AWS builds based off of package.json add the dependency
"dependencies": {
"body-parser": "^1.17.2"
}

Firebase reference is empty after requiring new version

The new documentation provides a straightforward method of initialising firebase, for example, this snipped comes directly from the README.md file:
Install the Firebase npm module:
$ npm init
$ npm install --save firebase
In your code, you can access Firebase using:
var firebase = require('firebase');
var app = firebase.intializeApp({ ... });
// ...
But in my installation this is not working, as in, firebase is empty ({} when console logged).
There's another section in the documentation called Include only the features you need, which provides the following snippet:
var firebase = require('firebase/app');
require('firebase/auth');
require('firebase/database');
var app = firebase.initializeApp({ ... });
// ...
This method works for me, so I'm wondering what could I be doing wrong in the first instance?
I'm using browserify, if that helps, but the docs state: The browser version is designed to be used with
a package bundler (e.g., Browserify,
Webpack).

How to set up an Express 4.0 application

I downloaded express today from npm, and to my surprise, it gave me express 4.0.0 instead of express 3.x.x.
I'm trying to configure a basic server with connect logger and body parser, but I'm not having much luck.
Could someone provide me with a boilerplate app.js file using express 4.0?
Thanks!
Got it!
You can get a skeleton app using:
$ npm install -g express-generator
Alternatively, you can swap out the connect logger and body parser for their connect standalone siblings (and it might be useful for learning what each middleware does, rather than relying on the generator to throw together a bunch of dependencies you may not need):
(based on Express 3.x to 4.x Migration guide)
var express = require('express');
var server = express();
...
server.use(require('body-parser')); //previously bodyparser (or rather urlencoded/json)
server.use(require('morgan')()); //previously connect-logger
...
server.listen('3000', function() {
console.log('server started');
});

Resources