![]() ![]() We can use this framework to build APIs, serve web pages, and other static assets and use it as a lightweight HTTP server and backend for our applications. ![]() app.js: const createError = require('http-errors') Ĭonst cookieParser = require('cookie-parser') Ĭonst usersRouter = require('./routes/users') Īpp.use(express.Express is a web application framework for Node.js. Here’s the main Express.js application file. We’ve pared down the lines of the remaining files that aren’t related to our goals. Yeoman can yield much better project skeletons in general, but for what we need, we’ll simply create a project skeleton with express-generator and remove the unnecessary parts, until we have this: bin ![]() What we won’t simplify is good project structure, the key to half the success of any project. But for our purposes, data storage specifics are unimportant, so we will mock them out for the sake of simplicity. In real projects, we would store the related data in some database like MongoDB. Let’s start with an Express.js tutorial application with a few routes for a user model. Typical Architecture for Express.js Routes ![]() We can avoid this antipattern (and its fallout) with the technique we’ll cover today-one I’ve used successfully in apps with hundreds of routes. With many Express.js tutorials leaving out that detail, developers often get in the habit of copying and pasting result-sending and error-handling code for each route, creating technical debt as they go. The Express.js tagline rings true: It’s a “fast, unopinionated, minimalist web framework for Node.js.” It’s so unopinionated that, despite current JavaScript best practices prescribing the use of promises, Express.js doesn’t support promise-based route handlers by default. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |