HowToCreateASailsJsApplicationWithSwagger is a project that shows how to setup Swagger-Node/ Swagger within a Sails.JS application.
- Install Node.JS properly (administrator mode on Windows)
- Check if the environment variables are set properly. (See image below for more information)
- Install Sails.JS:
npm install sails -g
- Install Swagger-Node/ Swagger:
npm install swagger -g
- Create the new project (didn't work for me but might.)
swagger project create yournameproject
Choose sails as framework in the following dialog option.
Run this step in Windows Powershell in Admin mode only. Otherwise it might fail: swagger-api/swagger-node#315
- Create the new project (Only if 5. didn't work)
sails new yournameproject
- Switch to your project folder
cd yournameproject
sails generate api user
This will generate a user model and controller.
- Install additional packages as you like:
npm install sails-mongo // Package for connecting to MongoDB (See https://github.com/balderdashy/sails-mongo)
npm install waterline --save // Generic database adapter for database-model abstraction (See https://github.com/balderdashy/waterline)
npm install winston --save // Package for logging (See https://github.com/winstonjs/winston)
npm install http-auth // Package for authentication for your service (See https://github.com/http-auth/http-auth)
npm install mqtt --save // Package to send/ receive MQTT messages (See https://github.com/mqttjs/MQTT.js)
-
Specifiy your options, e.g. database connections, logging, auth, ....
-
Copy the example swagger.yaml file from https://github.com/SeppPenner/HowToCreateASailsJsApplicationWithSwagger/tree/master/YAML to your service's api\swagger folder. (Only if 5. didn't work)
-
Adjust the config/models.js file like the following (or take a look at the https://github.com/SeppPenner/HowToCreateASailsJsApplicationWithSwagger/blob/master/exampleproject/config/models.js file):
migrate: 'safe'
to prevent sails from asking you if you want to delete your data in the database on every start of the service.
- Edit your Swagger API YAML file with
swagger project edit
-
Add the controller methods you defined in your controllers to the config/routes.js file. Please recognize the special notice below.
-
Add the content of the Swagger_Stuff folder to your service's assets folder.
-
Disable the default sails homepage view with the following configuration in your config/routes.js file (or take a look at the https://github.com/SeppPenner/HowToCreateASailsJsApplicationWithSwagger/blob/master/exampleproject/config/routes.js file):
'/':
{
// view: 'homepage'
},
- Check out your swagger ui on the http://localhost:10010 address with
swagger project start
Eventually, you will have to run a
npm install
or even
npm install
npm install sails -g
npm install swagger -g
first. The service should than run on http://localhost:10010 if you haven't specified a new port.
- The "winston" package needs a special (before created) file to log data to. It cannot create its own (empty) log file somehow!
- Never use / as the last char in a route in the Sails.JS config/routes.js file. This will crash your service! E.g. don't use something like 'post /api/user/register': 'UserController.registerUser/'. Use 'post /api/user/register': 'UserController.registerUser' instead.
- Make sure that the assets/index.html file and the swagger.yaml file both contain the exact same hostname/ uri (even localhost and 127.0.0.1 won't work!!!)
- You can easily clone the project and customize it :) -->
- Update the assets/index.html file.
- Update the swagger.yaml file by
swagger project edit
- Remove the package-lock.json file.
- Update via npm:
npm i -g npm-check-updates
npm-check-updates -u
npm install
- Configure Typescript according to https://sailsjs.com/documentation/tutorials/using-type-script
- Adjust your controllers and models to be *.ts instead of *.js
- Adjust your models and controllers according to https://github.com/aslanvaroqua/sails-ts/blob/master/api/controllers/WelcomeController.ts and https://github.com/aslanvaroqua/sails-ts/blob/master/api/models/Welcome.ts
- Start your app with node app.js instead of swagger project start or sails lift
- Change the host from http://localhost:10010/swagger to https://localhost:10010/swagger in the /assets/index.html file.
- Remove http from the schemes section in the swagger.yaml file.
- Uncomment (and change) the following lines as shown below in the /config/local.js file:
ssl: {
// ca: require('fs').readFileSync(__dirname + './ssl/my_apps_ssl_gd_bundle.crt'), //Do not use this line with self-signed certificates --> Uncommented
key: require('fs').readFileSync(__dirname + '/ssl/default.key'),
cert: require('fs').readFileSync(__dirname + '/ssl/default.crt')
},
and
port: process.env.PORT || 10010,
- Add your generated ssl files (you might need to replace your SERVER_NAME in this script)
export SERVER_NAME='localhost'
mkdir ssl
openssl req -nodes -x509 -newkey rsa:2048 \
-subj "/CN=$SERVER_NAME" \
-keyout ssl/default.key \
-out ssl/default.crt
to the config folder: /config/ssl/
- Now your server should run under https://localhost:10010/
See https://github.com/chriso/validator.js for examples.
See the Changelog.