Converting a mock e-commerce PSD design into HTML, CSS.
- Use a mock E-commerce Photoshop design
- Use Bootstrap to make the responsive of the site
- Use Bootstrap Carousel
- Use NodeJS for the server
- Easy to build
- Cheap to host (HTML, CSS)
- Fixed content
- Little hard to build
- Costly than static server host
- Easily edit content by their own
- PHP: Laravel, Symfony, CakePHP, etc.
- Java: Spring, Play Framework, etc.
- Python: Django, Flask, etc.
- Ruby: Rails
- JavaScript: ExpressJS, SailsJS, MeteorJS, KoaJS, etc.
- Content rendered in the front-end
- No page reloading
- Interact with data via some JSON API
- Receive a request from client and response a HTML
- Use page reloading
- Use form action
- Use GET method
Create a search form (pug)
form(action="/users/search", method="GET")
input(type="text", name="q")
button Search
Then add logic for searching the typed text
app.get("/users/search", (req, res) => {
let q = req.query.q;
let matchedUsers = users.filter(user => {
return user.name.toLowerCase().indexOf(q.toLowerCase()) !== -1;
})
res.render('users/index', {
users: matchedUsers
})
});
- Use form action
- Use POST method
- Use req.body
Create a form
form(action="/users/create", method="POST")
label Name
input(type="text", name="name")
button Create
Then use body-parser middleware to retreive POST query parameters
npm install body-parser --save
const bodyParser = require("body-parser");
...
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
...
app.post("/users/create", (req, res) => {
users.push(req.body);
res.redirect("/users");
});
npm install lowdb --save
const low = require("lowdb");
const FileSync = require("lowdb/adapters/FileSync");
const adapter = new FileSync("db.json");
const db = low(adapter);
db.defaults({ users: [], products: [] }).wriet();
...
app.post("users/create", (req, res) => {
db.get("users").push(req.body).write();
res.redirect("/users");
});
app.get("users/search", (req, res) => {
let q = req.query.q;
let users = db.get("users").value();
let matchedUsers = users.filter(user => {
return user.name.toLowerCase().indexOf(q.toLowerCase()) !== -1;
});
res.render('users/index', {
users: matchedUsers
});
});