Node
- Node is a JavaScript engine that you can run from the command line.
- nvm is “node version manager”. Helpful if you work on projects that are picky about their node version.
- Show a simple ‘hello world’ app in a clean directory.
NPM
npm
is the N
ode P
ackage M
anager. It makes it easy to install libraries for your use.
- If you are downloading/cloning an existing project that has a
package.json
file, run npm install
.
- If you are building a new project from scratch, you must begin with
npm init
. (Accept the defaults for now.)
- This creates a
packages.json
file describing the app and its dependencies.
- At a high level, there are two types of packages:
- Libraries containing code to import into your own JS projects.
- Command-line tools written in JavaScript.
- Use
npm install foo
to install packages.
- By default, packages are installed locally to your
node_modules
directory.
- Adding the
--save
or -save-dev
flag will update the dependency list in node_modules
--save
is for packages used in production. --save-dev
is for things like testing frameworks and linters that are only used in development.
- Adding the
-g
flag will install the module globally (i.e., make it accessible to all your projects.)
- I would not use
-g
for libraries: It could lead to version conflicts.
- You may consider using
-g
to install command-line scripts
- If you choose to install a command-line script locally, (without the
-g
flag) you can access it either
- through
node_modules/.bin
(e.g., ./node_modules/.bin/ejs-cli
), or
- using
npx
(e.g., npx ejs-cli
)
Express
- Begin with a quick overview of asynchronous programming
- Express is a web server written in Javascript.
- Download from
https://expressjs.com/
- Basic idea: You instantiate a server object, then provide callbacks that determine
the response for each path on your site.
- Show
simple_sendHTML.js
- Add sending of a static file.
- Add
app.use(express.static('images'))
- Notice that you just say
http://localhost:3000/buzz2.jpeg
, URL does not include images
- Order of routes in the code matters. First route to match gets executed.
- Create a route named
buzz2.jpeg
- Put it before the
express.static
.
- Put it after the
express.static
.
- Show how to render
.ejs
- Show how query string is parsed.
- Put the two together.
- Routes can be regular expressions.
- Look at Wordle starter code.