- Create new repo
- Add README and node gitignore
- Add contributors
touch server.js
npm init -y
(check to see the package.json was created)npm install express
- Boilerplate the server
const express = require("express");
const app = express();
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
- Add middleware for POST body.
- Add
/api/config
route to test.
npm install express-handlebars
- Require express-handlebars in server.js
- Add the handlebars middleware (view engine, etc. )
app.engine("handlebars", exphbs({ defaultLayout: "main" }));
app.set("view engine", "handlebars");
-
Setup your views directory with proper sub folders. | ----- views | ----- layouts | | | ------- main.handlebars ----- index.handlebars
-
Add boilerplate and "triple-stache" into main.handlebars
-
(Optional) Include your CSS Library and jQuery
-
Add "Hello World" to the index.handlebars
-
Add a server route to test the index.handlebars file.
app.get("/", (req, res) => {
res.render("index");
});
npm install sequelize mysql2
sequelize init:config & sequelize init:models
(Confirm we see config and models directories)- Create a db folder and add schema.sql
DROP DATABASE IF EXISTS starter_db;
-- Creates the "starter_db" database --
CREATE DATABASE starter_db;
USE starter_db;
- Modify config/config.json with password and database name in development.
- Create your first model.
module.exports = function (sequelize, DataTypes) {
const Thing = sequelize.define("Thing", {
name: {
type: DataTypes.STRING,
},
price: {
type: DataTypes.DECIMAL,
},
});
return Thing;
};
- Import db into server.js
- Connect the database to our server, by wrapping app.listen in
db.sequelize.sync()
- Create a new controller for your model.
- Include basic boilerplate code:
const express = require("express");
const router = express.Router();
module.exports = router;
- Setup Views routes and API routes
- View Routes may include:
- all-things
- single-thing
- edit-thing
- new-thing
- Import db into our controller and populate the page.
- Getting error:
Access has been denied to resolve the property "name" because it is not an "own property" of its parent.
?
- Run
npm install handlebars @handlebars/allow-prototype-access
- Modify your server:
const handlebars = require("handlebars");
const {
allowInsecurePrototypeAccess,
} = require("@handlebars/allow-prototype-access");
- Modify the
app.engine
app.engine(
"handlebars",
exphbs({
defaultLayout: "main",
handlebars: allowInsecurePrototypeAccess(handlebars),
})
);