I am new to node and building a simple book library app with express and mongodb. My forms work fine but I have having trouble with form validations. I initially used express validator but seems the code I wrote from the older version and it's legacy now. The problem is there are so many frameworks(Joi, express validator with joi, form input validator) and I am little confused now. One good thing with Java/Spring is that you don't have so many options. Can you please suggest what should be the best way to do that.
Thanks in advance.
There are two types of form validations client-side and server-side If you want to validate from server-side. You can use another node package to validate your form,
https://www.npmjs.com/package/form-validate
if you want to validate from client-side you can use jQuery plugin,
https://jqueryvalidation.org/
Related
I am looking for a way to generate OpenAPI/Swagger API definitions from code written in Node.JS/Express.JS/Typescript.
Ideally this would be just annotations the I had to my Express Typescript base controllers and this generate the OpenApI/Swagger by running some sort of command line, this way the API definition would always stay in sync with the actual implementation, and additionally this tool should generate some sort of middleware that can implement parameter validation of the controllers input parameters.
Thank you, kindly
Oscar
If you want swagger generated for you, you can make use of NestJs.
NestJs also provides inbuilt middle ware for validation.
If you wanna know more visit this link
I inherited an existing API and I would like to document it with swagger, but I don't yet know the full scope of it. Can Swagger (or another middleware/tool) auto-magically generate the yaml (for swagger) based on the existing express routes?
For what I saw on other questions, it would appear that this is mostly a manual job, but I'm double-checking if someone here found a way around this.
I have experience in BOTH auto-generating the Swagger json and manually writing it out for an API that I helped build. Here are the pros/cons of both based on my experience.
Swagger AUTOMATIC Documentation Generation:
We used the swagger-node-express module in combination with swagger-ui.
https://www.npmjs.com/package/swagger-node-express
https://github.com/swagger-api/swagger-ui
Pros
Super easy to document. Just throw a few lines above the resource definition and the documentation (json) is automatically generated by the module.
Cons
You are no longer using straight up Express when you use this package. Your route definitions have to be defined through the Swagger module and this pulls you away from vanilla Express.
Swagger MANUAL Documentation Generation:
We just pulled swagger-ui into the project and wrote the documentation manually.
https://github.com/swagger-api/swagger-ui
Pros
This approach decouples the documentation from the Express framework. Express endpoints are written as they normally would be written and the Swagger documentation is defined separate from the Express framework. Allows you to write pure express.
Cons
Documentation changes become a little more tedious due to the fact that you are manually writing and changing the yaml or json yourself. It's a little bit harder than just updating a few lines of code above a resource. This approach is also a little more prone to documentation typos and errors due to the fact it is entirely manually typed.
If you are planning to manually write your swagger documentation use the swagger editor below to validate your manual docs.
http://editor.swagger.io/#/
Conclusion
For this API project, we started out by auto-generating the documentation using the swagger-node-express package. However, we realized that decoupling the swagger documentation from the express library was important to enable us to use all the features and functionality of Express. I recommend manually writing the docs to have full control over both the Swagger documentation and the Express web framework that your app will use.
There is an option: you can embed middleware that will analyse all requests and responses and generate specification for you: https://github.com/mpashkovskiy/express-oas-generator
Then you can use it through your's app Swagger UI like http://host:port/api-docs
Yes !!!. You can use this awesome project typescript-test. Here is sample app. Clone it, run npm i,npm run swagger and go to /dist/swagger.json. Done. Swagger yaml and json is generated based on express routes !
With express-sitemap-html you may automatically generate a minimalistic Open API definition (only including route parameters) and install a Swagger UI for all routes of an existing express app. You only need:
const sitemap = require('express-sitemap-html')
...
sitemap.swagger('Your app name', app) // given that app is an express instance
Instead of analyzing HTTP requests at runtime, this approach inspects express app instance and mounted routes.
PROs you don't need to perform ahead requests to get an updated list of available routes.
CONs it provides untyped parameters features.
Have a look to swagger-jsdoc. It's a different approach.
The docs stick to the code, and also lets the express code to remain pure.
Guides:
https://dev.to/acanimal/express-api-with-autogenerated-openapi-doc-through-swagger-7na
https://dev.to/akshendra/generating-documentation-on-the-fly-in-express-2652
Given the promise Polymer and web components have in simplifying development I am wondering if anyone has developed a wrapper component that simplifies talking to a MongoDB backend? Does anyone know of one?
Is this a sane idea? I guess it would need a custom server-side piece, but even so, I expected that lots of people would have written one, but I cannot find any; or is this a case where there are so many options that I should write my own node/express/mongo server?
Or is it that the AJAX component will do all the heavy lifting and I just need to use that?
Puzzled Andy
I'm unaware of a MongoDB element, but perhaps you could use the Polymer firebase element as a reference.
https://github.com/polymer/firebase-element
I think that you should use Strongloop/Loopback (http://loopback.io/getting-started/) as backend and do ajax request thought Fetch (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch).
Unfortunally Fetch isn't fully supported in all browser but thanks to this polyfill (https://github.com/github/fetch) you can have a full supported fallback solution.
I'm new to Node.js I'm trying to transfer all I did before to this environment.
I'm using Express.js with Mongoose.
I come from a ASP.NET MVC environment where I'm used to define my model and then automatically scaffolding the form with personalized HTML.
I'd like to know if there's a way to scaffold form for edit insert and a way to go for input validation on single fields.
I've looked here http://plugins.mongoosejs.com but I didn't find what I needed.
If you are just starting the study Express, then play with express-validator with this micro tutorial
or use Mongo's build in validators
I'm extremely new to node.js, and have years of experience in PHP, so my thinking about the issues below, might be tainted by my backed ways.
I'm currently using node set up with foundation CSS, and using their reveal modal windows to display most of my forms. I want to avoid re-displaying them upon submit, and instead, validate them on client side.
Basic user creation will have fields as per below:
name
email
password
password confirmation
Form my basic validation i can use require to validate the input,
input(type="text",name="user[email]",placeholder="#",required)
but I would also like the client to check if the email already exists after the user types it in, and whether the passwords are correct.
I've been reading about the validator module but from the documentation I read about it, I only understand how to validate the values after they've been posted. I've seen the express-form module as well, but It's no longer maintained, so I would rather avoid it.
Could someone point me in the right direction?
I don't have any experience with validator or express-form, and I am not sure you need them for what you are trying to achieve. Most of what you are trying to do will be client side work (e.g. jQuery Ajax calls to validate the data) That kind of client-side code should be pretty much the same regardless of whether you use a Node.js backend or a PHP backend.
Injecting the initial JavaScript in your Jade page should be pretty straightforward once you figure out the proper syntax to include JavaScript in a Jade page. The wiring of DOM events on the page to make Ajax calls (onClick, onTextChanged, et cetera) will be conceptually the same as if you were writing a plain HTML page.
In your Node.js server side you'll need a route to handle the request to validate if an e-mail address already exists and that code very likely will return JSON that you will use in your client side form to display the results of the validation to the user. But that should also be very similar to what you would have done with a PHP backend.