// For importing: import package from 'package' becomes
const package = require('packageName or path');
// can be deconstructed, like: import { partpackage } from 'package'
const { subPackage } = require('packageName or path');
// For exporting: export default becomes
module.exports = something; //
// export <something> becomes
exports.something1 = something1;
exports.something2 = something2;
// Unlike es6, you can't use module.exports AND exports.<thing> together
...but we'll do it manually first
const express = require('express');
const app = express();
// Good Practice, see next slide
const PORT = process.env.PORT || 3333;
// This starts the server
// Later this part goes in index.js
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});
To run this file, go to your command line and do:
npm start
(in your server.js)
const express = require('express');
const app = express();
// Good Practice
const PORT = process.env.PORT || 3333;
app.use(express.static('public'));
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3333;
app.use(function myLogger(req, res, next) {
console.log('LOGGED')
next(); // passes the request on to next middleware
});
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3333;
app.use(express.static('public'));
const cars = [{name: 'ferarri'}, {name: 'lamborghini'}];
app.get('/cars', function(req, res){
return res.json(cars);
});
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});
const express = require("express");
const app = express();
const PORT = process.env.PORT || 3333;
app.use(express.static("public"));
// parse application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: false }));
app.use(express.json()); // parse application/json
const cars = [{ name: "ferarri" }, { name: "lamborghini" }]; // In memory
app.get("/cars", function(req, res) {
return res.json(cars);
});
app.post("/cars", function(req, res) {
cars.push(req.body);
return res.sendStatus(201);
});
// ....Delete and Update go here
app.listen(PORT, function() {
console.log(`Server is listening on port ${PORT}`);
});
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3333;
app.get('/', function(req, res) {
if(!req.user) res.redirect('/login');
// other code coes here
});
app.listen(PORT, function() {
console.log(`Server is listening on port ${PORT}`);
});
app.get('/users/:userId/books/:bookId?', function (req, res) { // ? = optional
res.send(req.params)
});
/*
* Route path: /users/:userId/books/:bookId
* Request URL: http://localhost:3000/users/34/books/8989
* req.params: { "userId": "34", "bookId": "8989" }
*/
Query Params:
// GET /search?q=tobi+ferret
req.query.q
// => "tobi ferret"
const petsRouter = express.Router([options]); // local
const carsRouter = require('./routers/cars/');//separate file
const dogs = [{}, {}];
// Triggered by URL /pets/dogs
petsRouter.get('/dogs', function (req, res, next) {
res.status(200).send(dogs);
});
app.use('/cars', carRouter);
app.use('/pets', petRouter);
const express = require('express');
const bodyParser = require('body-parser');
const es6Renderer = require('express-es6-template-engine');
const app = express();
const PORT = process.env.PORT || 3333;
app.use(express.static('public'));
app.engine('html', es6Renderer); // give a renderer a name
app.set('views', 'views'); // needs a 'views' dir with the templates in
app.set('view engine', 'html'); // set the renderer called 'html' as the view rendering program
app.get('/', function(req, res) {
res.render('index', {
locals: {
title: 'Welcome!',
},
partials: {
header: 'header',
footer: 'footer',
},
}, (err, html) => {
if (err) {
res.status(500).send(err);
} else {
res.send(html);
}
});
});
app.listen(PORT, function(){
console.log('Server is listening');
});