Created
June 16, 2020 19:17
-
-
Save AlexSKuznetsov/486d2ec696e98c5c85e4ab85d791f6f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------ БАЗА ----- // | |
| const express = require('express') | |
| const app = express() | |
| app.get('/', (req, res) => res.send('Hello World!')) | |
| app.listen(process.env.PORT || 3000) | |
| /// --------СТАТИКА ------ /// | |
| // подключение статической директории | |
| app.use(express.static('public')) | |
| // для создания виртуальной статической директории | |
| app.use('/static', express.static('public')) | |
| // или | |
| app.use('/static', express.static(path.join(__dirname, 'public'))) | |
| /// ----- ПОДКЛЮЧЕНИЕ HBS ----- //// | |
| app.set('view engine', 'hbs') | |
| /// ---- РОУТИНГ ---- //// app.METHOD(PATH, HANDLER) | |
| // пример гет запроса | |
| app.get('/', function (req, res) { | |
| res.send('hello world') | |
| }) | |
| // обработка параметров в запросе | |
| // Route path: /users/:userId/books/:bookId | |
| // Request URL: http://localhost:3000/users/34/books/8989 | |
| // req.params: { "userId": "34", "bookId": "8989" } | |
| app.get('/users/:userId/books/:bookId', function (req, res) { | |
| res.send(req.params) | |
| }) | |
| // подключение роутера в отдельном файле | |
| const route = express.Router() | |
| // какой то метод | |
| module.exports = route; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment