While refreshing my skill set, I became acquainted with the ExpressJS framework. Different frameworks share similar fundamental concepts. Here, I document the development process starting from the basic initial structure.
1. Install basic expressJS project
npm install express-generator express --view=jade 2403_express_sampleexpress_sample cd 2403_express_sampleexpress_sample npm install express
npm install mongoose
npm run start ( Ctrl+c to stop )
2. Checking the ExpressJS structure
2-1. The startup file for ExpressJS.
In ./package.json, find "scripts":{}. Among them, "start" records the startup file.
Add another startup file: ”dev": "node ./test/loc". Then create loc.js file on the corresponding file path. This option is for testing, the way to run test:
npm run dev
2-2. Find the port of expressJS service
In the startup file `./bin/www.js` for ExpressJS, searching for "port" reveals a comment, "Get port from environment and store in Express," indicating the default port used by Express is 3000.
When ExpressJS is running, using a browser to navigate to "http://localhost:3000/" will display the default page.
2-3. expressJS routing
All expressJS's routing modules are in ./routes folder. Default Pages are index.js and user.js
page URIs are set in ./app.js
Searching for "routes/index" in app.js reveals that modules from the `./routes` directory are individually added.
After "routes/users" is loaded, it is named `usersRouter`, and when integrated into the main structure, its URI is set to '/users'.
app.use('/users', usersRouter);
Opening `./routes/users.js`, you'll notice that the router doesn't handle the module name.
router.get('/', function(req, res, next) ...
The router handles subsequent parameter values.
router.get('/:id', async function(req, res) { console.log("book id: "+ req.params.id)
3. Build nodeJS module
3-1. Initialize module
mkdir -p modules/bookmgr cd modules/bookmgr npm init
At this stage, npm prompts a series of questions to help developers set up the basic parameters needed for the module. The default name of the module is the directory name.
One of the questions is about the startup file. npm only helps to create package.json, which will start from the startup file recorded in the json.
3-2. Build object within the module
Since modules are obtained using "require" and not "import," to be considered part of a module, an object needs to be inside the module and acquired via "require."
var somename = require([relative path]);
"require" doesn't create a new instance, so to create an instance of a class, you need to initialize it yourself, and the framework may not execute it immediately. This results in module internal console.log() being immediate, but this.log being slightly delayed.
Setting up a consistent log variable within the module makes it easier to manage, though it might not be very useful for real-time debugging. Before the module development stabilizes, there will likely be many scattered console.log() statements.
Notes for module:
module.exports : Only with it can the main structure locate this module.
exports.init: Only with it can other files access the public functions. The module does not use export.
3-3. Install module
Back to the root of expressJS project. (Here is 2403_express_sampleexpress_sample/)
npm install ./modules/bookmgr ;
After installation, the module appears in ./node_modules/bookmgr.
All subsequent changes in the original directory (./modules/bookmgr) need to be updated to node_modules by install.
sample code: https://github.com/atfuture7/testcode/tree/master/nodejs/10_mod_nodjs/
Comments
Post a Comment