Instructions
node.js
Express
Use the application generator tool, express-generator, to quickly create an application skeleton.
I run these instructions on windows 10(2004)
, node v12.18.4
and npm 6.14.8
Install Express Generator in AppData\Roaming\npm\node_modules
The -g or –global argument will cause npm to install the package globally rather than locally.
Cheke parameters by running:
then you will see the result like this:
Specify express
to create a project
You can choose a view (template) engine using –view and/or a CSS generation engine using –css. Here I choose to use EJS(Embedded JavaScript) and sass.
Note: Change directory to your project. If you haven't created it, then you can add the project name at the tail of the following command.
Install all the packages
This just like requirements.txt
in Python. With generated package.json
, when you move the project into another environment, you can just run npm install
(without any package name) then the npm will install all the dependency that your project needs.
Normal Running
Debug mode
On Windows, use this command(by MDN Doc):
but I failed. I found that it consist of two parts, so I ran in two steps:
then
On macOS or Linux, use this command:
I haven't try it in Ubuntu.
Any changes you make to your Express website are currently not visible until you restart the server. So, we should install nodemon
to enable server restart on file changes.
Install nodemon
I installed and used it locally as a developer dependency, so that any developers working with the project get it automatically when they install the application.
-D, –save-dev: Package will appear in your devDependencies.
Or, you can choose to install it globally. It depends on you.
After installing, you can find it in package.json
:
Update it by putting a comma at the end of that line, and adding the "devstart" and "serverstart" lines
Run the project with nodemon for developing
www file
/bin/www
is the entry of the application. The first thing it does is to require('../app')
which is app.js
in the root, the "real" entry point.
MDN Docs:
require()
is a global node function that is used to import modules into the current file. Here we specify app.js module using a relative path and omitting the optional (.js) file extension.
okay, MDN Web Docs provide more information about.
Port is already in use
I got this trouble when I was launching Browser Sync
from Sublime Text to show my front-end design. The Browser Sync
uses port 3000, and the default port of express is also port 3000. There are two ways to deal with it, one is to close Browser Sync
, two is to set another express port.
I chose to close Browser Sync
but I couldn't find it in task manager.
So, I tried to find its PID by running this (windows 10):
and killed the process by its PID:
npm-install
Express Tutorial Part 2: Creating a skeleton website
[Node.js]Express GeneratorーWeb應用程式產生器的使用方式
不要踏到 Node.js 的黑洞陷