1.0.4 • Published 2 years ago
@jay-law/hello-world-expressjs v1.0.4
Creating the App
# Create package.json scoped to my username
$ npm init --scope=@jay-law
# Build out project structure
$ npx express-generator
# npx requires npm to be installed
# Install dependencies
$ npm install
# Start server (debug mode)
$ DEBUG=hello-world-expressjs:* npm start
# or
$ npm start
# or
$ nodemon
# requires `nodeman` package
# npm install -g nodemon
Testing
See routes/index.js
for the available URL calls.
Publishing
Publish to Git
# Commit changes
$ git add .
$ git commit -m 'commit message'
# Incriment version
$ npm version (major|minor|patch)
# Push (npm creates the tags automatically)
$ git push --follow-tags
Publish to NPM
Manually Publish
# Ensure you are logged in
$ npm login
# Remove "private: true" line from package.json
# Publish
$ npm publish --access public
Confirm it exists - https://www.npmjs.com/package/@jay-law/hello-world-expressjs
Auto Publish
Enable token publishing for package
- Navigate to the package page - https://www.npmjs.com/package/@jay-law/hello-world-expressjs
- Settings -> Publishing access -> Select 'Require two-factor authentication or automation tokens'
- Select 'Update Package Settings'
- Be sure to create an access token for your user if needed. Guide - https://docs.npmjs.com/creating-and-viewing-access-tokens
Automate publishing in GitHub
- Navigate to the git repo
- Select 'Actions'
- Select 'Publish Node.js Package' then 'Configure'
- Make changes as needed to the pipeline
- Get the
NODE_AUTH_TOKEN
variable name. It should be something like${{secrets.npm_token}}
- Select 'Start commit' to save
- Go to Settings -> Secrets -> Actions
- Select 'New repository secret'
- Add
npm_token
for the token name and your npm token for the value
Test the workflow/pipeline
- Do a
git pull
locally to pull down the .github dir. It contains the workflow config - Do the normal git stuff then push
- Assuming all work so far has been on the main branch, a GitHub action should have been triggered
- Confirm the publish on the NPM site
- Do a
Pushes directly to the master branch should be disabled when coding in "real" environments.
Errors
Error - 'address already in use :::3000'
Node might keep port binding. Confirm with ps
then kill the process with pkill -9 node --echo