Creating a Web Site with Express Generator
Express web site step by step recommended https://expressjs.com/en/starter/generator.html
Original Tutorial https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/skeleton_website
Create your Project Directory
Open up command prompt at the directory level
type in following commands
express express-locallibrary-tutorial --view=pug
cd express-locallibrary-tutorial
npm install
PS C:\libstart> express express-locallibrary-tutorial --view=pug
create : express-locallibrary-tutorial\
create : express-locallibrary-tutorial\public\
create : express-locallibrary-tutorial\public\javascripts\
create : express-locallibrary-tutorial\public\images\
create : express-locallibrary-tutorial\public\stylesheets\
create : express-locallibrary-tutorial\public\stylesheets\style.css
create : express-locallibrary-tutorial\routes\
create : express-locallibrary-tutorial\routes\index.js
create : express-locallibrary-tutorial\routes\users.js
create : express-locallibrary-tutorial\views\
create : express-locallibrary-tutorial\views\error.pug
create : express-locallibrary-tutorial\views\index.pug
create : express-locallibrary-tutorial\views\layout.pug
create : express-locallibrary-tutorial\app.js
create : express-locallibrary-tutorial\package.json
create : express-locallibrary-tutorial\bin\
create : express-locallibrary-tutorial\bin\www
change directory:
> cd express-locallibrary-tutorial
install dependencies:
> npm install
run the app:
> SET DEBUG=express-locallibrary-tutorial:* & npm start
PS C:\libstart> cd express-locallibrary-tutorial
PS C:\libstart\express-locallibrary-tutorial> npm install
npm notice created a lockfile as package-lock.json. You should commit this file.
added 118 packages from 174 contributors and audited 247 packages in 3.478s
found 1 low severity vulnerability
run `npm audit fix` to fix them, or `npm audit` for details
PS C:\libstart\express-locallibrary-tutorial>
PS C:\libstart\express-locallibrary-tutorial> SET DEBUG=express-locallibrary-tutorial:*
PS C:\libstart\express-locallibrary-tutorial> mpm start
Create App
After you install Express you can create a simple app.
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
This app starts a server and listens on port 3000 for connections. The app responds with “Hello World!” for requests to the root URL (/
) or route. For every other path, it will respond with a 404 Not Found.
//require the just installed express app
var express = require('express');
//then we call express
var app = express();
//takes us to the root(/) URL
app.get('/', function (req, res) {
//when we visit the root URL express will respond with 'Hello World'
res.send('Hello new World!');
});
//the server is listening on port 3000 for connections
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
});
Now we can test our server by running:
node index.js
// Example app listening on port 3000!
And if you open your browser and visit: localhost:3000
and you should see Hello World!