express-sweet-generator 2.0.2 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +168 -9
  3. package/package.json +2 -3
  4. package/screencaps/demonstration.webp +0 -0
  5. package/templates/cjs/app.js +2 -2
  6. package/templates/cjs/config/authentication.js +0 -1
  7. package/templates/cjs/config/config.js +1 -4
  8. package/templates/cjs/config/database.js +3 -3
  9. package/templates/cjs/config/view.js +0 -1
  10. package/templates/cjs/models/BookModel.js +2 -2
  11. package/templates/cjs/models/CommentModel.js +2 -2
  12. package/templates/cjs/models/ProfileModel.js +2 -2
  13. package/templates/cjs/models/UserModel.js +2 -2
  14. package/templates/cjs/package.json.ejs +1 -1
  15. package/templates/cjs/routes/api/users.js +4 -4
  16. package/templates/cjs/routes/login.js +1 -1
  17. package/templates/cjs/routes/users.js +2 -2
  18. package/templates/esm/app.js +2 -2
  19. package/templates/esm/bin/www.ejs +1 -1
  20. package/templates/esm/config/authentication.js +1 -2
  21. package/templates/esm/config/config.js +2 -5
  22. package/templates/esm/config/database.js +3 -3
  23. package/templates/esm/config/view.js +1 -2
  24. package/templates/esm/ecosystem.config.js.ejs +0 -2
  25. package/templates/esm/models/BookModel.js +2 -2
  26. package/templates/esm/models/CommentModel.js +2 -2
  27. package/templates/esm/models/ProfileModel.js +3 -3
  28. package/templates/esm/models/UserModel.js +6 -6
  29. package/templates/esm/package.json.ejs +4 -6
  30. package/templates/esm/routes/api/users.js +6 -7
  31. package/templates/esm/routes/login.js +1 -1
  32. package/templates/esm/routes/users.js +2 -2
  33. package/templates/esm/shared/CustomValidation.js +1 -1
  34. package/QUICK_START.md +0 -158
  35. package/screencaps/login.jpg +0 -0
  36. package/screencaps/personal.jpg +0 -0
  37. package/screencaps/user-editing.jpg +0 -0
  38. package/screencaps/users.jpg +0 -0
package/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2022 Takuya Motoshima
1
+ Copyright 2024 Takuya Motoshima
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
4
 
package/README.md CHANGED
@@ -1,16 +1,175 @@
1
- # Express Sweet
2
- [Express Sweet](https://www.npmjs.com/package/express-sweet) application generator.
1
+ # EXPRESS SWEET
2
+ [EXPRESS SWEET](https://www.npmjs.com/package/express-sweet)'s application generator.
3
3
 
4
- ## Documentation
5
- The latest API reference for Express Sweet can be found [here](https://takuya-motoshima.github.io/express-sweet/v2/).
4
+ ## Installation
5
+ ```sh
6
+ npm install -g express-sweet-generator
7
+ ```
6
8
 
7
- The API reference for previous versions can be found [here](https://takuya-motoshima.github.io/express-sweet/v1/).
9
+ ## Quick Start
10
+ Use the application generator tool, `express-sweet-generator`, to quickly create an application skeleton.
8
11
 
9
- ## Release Notes
10
- All changes can be found [here](https://github.com/takuya-motoshima/express-sweet/blob/HEAD/CHANGELOG.md).
12
+ ![Image](screencaps/demonstration.webp)
11
13
 
12
- ## Quick Start
13
- See [here](QUICK_START.md).
14
+ 1. For example, the following creates an Express app named `myapp`. The app will be created in a folder named `myapp` in the current working directory.
15
+ ```sh
16
+ express-sweet -o esm myapp
17
+ ```
18
+ 1. Then install dependencies.
19
+ ```sh
20
+ cd myapp/
21
+ npm install
22
+ ```
23
+ 1. The skeleton uses a DB. Please create a DB with the following SQL.
24
+ ```sql
25
+ CREATE DATABASE IF NOT EXISTS `sample_db` DEFAULT CHARACTER SET utf8mb4;
26
+
27
+ USE `sample_db`;
28
+
29
+ CREATE TABLE `user` (
30
+ `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
31
+ `name` varchar(30) NOT NULL,
32
+ `email` varchar(255) NOT NULL,
33
+ `password` varchar(100) NOT NULL,
34
+ `icon` varchar(768) NOT NULL DEFAULT MD5(RAND()),
35
+ `created` datetime NOT NULL DEFAULT current_timestamp(),
36
+ `modified` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
37
+ PRIMARY KEY (`id`),
38
+ UNIQUE KEY `ukUserEmail` (`email`),
39
+ UNIQUE KEY `ukUserIcon`(`icon`)
40
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
41
+
42
+ CREATE TABLE `profile` (
43
+ `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
44
+ `userId` int(10) unsigned NOT NULL,
45
+ `address` varchar(255) NOT NULL,
46
+ `tel` varchar(14) NOT NULL,
47
+ `created` datetime NOT NULL DEFAULT current_timestamp(),
48
+ `modified` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
49
+ PRIMARY KEY (`id`),
50
+ UNIQUE KEY `ukProfileUserId` (`userId`),
51
+ CONSTRAINT `fkProfileUser` FOREIGN KEY (`userId`) REFERENCES `user` (`id`)
52
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
53
+
54
+ CREATE TABLE `comment` (
55
+ `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
56
+ `userId` int(10) unsigned NOT NULL,
57
+ `text` text NOT NULL,
58
+ `created` datetime NOT NULL DEFAULT current_timestamp(),
59
+ `modified` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
60
+ PRIMARY KEY (`id`),
61
+ CONSTRAINT `fkCommentUser` FOREIGN KEY (`userId`) REFERENCES `user` (`id`)
62
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
63
+
64
+ CREATE TABLE `book` (
65
+ `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
66
+ `userId` int(10) unsigned NOT NULL,
67
+ `title` text NOT NULL,
68
+ `created` datetime NOT NULL DEFAULT current_timestamp(),
69
+ `modified` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
70
+ PRIMARY KEY (`id`),
71
+ UNIQUE KEY `ukBookTitle` (`userId`, `title`(255)),
72
+ CONSTRAINT `fkBookUser` FOREIGN KEY (`userId`) REFERENCES `user` (`id`)
73
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
74
+
75
+ INSERT INTO `user` (`id`, `email`, `password`, `name`, `icon`) VALUES
76
+ (1, 'robin@example.com', 'password', 'Robin', '/upload/1.png'),
77
+ (2, 'taylor@example.com', 'password', 'Taylor', '/upload/2.png');
78
+ INSERT INTO `profile` (`userId`, `address`, `tel`) VALUES
79
+ (1, '777 Brockton Avenue, Abington MA 2351', '202-555-0105'),
80
+ (2, '30 Memorial Drive, Avon MA 2322', '');
81
+ INSERT INTO `comment` (`userId`, `text`) VALUES
82
+ (1, 'From Robin #1'),
83
+ (1, 'From Robin #2'),
84
+ (2, 'From Taylor #1');
85
+ INSERT INTO `book` (`userId`, `title`) VALUES
86
+ (1, 'Beautiful'),
87
+ (1, 'Lose Yourself'),
88
+ (2, 'When Im Gone');
89
+ ```
90
+ 1. Set the DB connection method in `config/database.js`. For details, please refer to [here](https://sequelize.org/docs/v6/other-topics/migrations/#cfg).
91
+ ```js
92
+ export default {
93
+ development: {
94
+ username: 'root',
95
+ password: 'password',
96
+ database: 'sample_db',
97
+ host: 'localhost',
98
+ dialect: 'mariadb'
99
+ },
100
+ test: {
101
+ username: 'root',
102
+ password: 'password',
103
+ database: 'sample_db',
104
+ host: 'localhost',
105
+ dialect: 'mariadb'
106
+ },
107
+ production: {
108
+ username: 'root',
109
+ password: 'password',
110
+ database: 'sample_db',
111
+ host: 'localhost',
112
+ dialect: 'mariadb'
113
+ }
114
+ }
115
+ ```
116
+ 1. The DB to be accessed can be defined for each environment. Specify the environment in the `.env` file
117
+ ```sh
118
+ NODE_ENV=development
119
+ ```
120
+ 1. Run the application with the following command.
121
+ ```sh
122
+ npm start
123
+ ```
124
+ 1. Then, load `http://localhost:3000/` in your browser to access the app.
125
+ The generated app has the following directory structure:
126
+ ```sh
127
+ .
128
+ ├── .env
129
+ ├── app.js
130
+ ├── ecosystem.config.js
131
+ ├── nginx.sample.conf
132
+ ├── package.json
133
+ ├── bin
134
+ │ └── www
135
+ ├── client
136
+ │ ├── package.json
137
+ │ ├── webpack.config.js
138
+ │ └── src
139
+ ├── config
140
+ │ ├── authentication.js
141
+ │ ├── config.js
142
+ │ ├── database.js
143
+ │ └── view.js
144
+ ├── exceptions
145
+ │ └── UserNotFound.js
146
+ ├── models
147
+ │ ├── BookModel.js
148
+ │ ├── CommentModel.js
149
+ │ ├── ProfileModel.js
150
+ │ └── UserModel.js
151
+ ├── public
152
+ │ ├── build
153
+ │ └── upload
154
+ ├── routes
155
+ │ ├── login.js
156
+ │ ├── users.js
157
+ │ └── api
158
+ │ └── users.js
159
+ ├── shared
160
+ │ ├── CustomValidation.js
161
+ │ └── empty.js
162
+ └── views
163
+ ├── edit-personal.hbs
164
+ ├── error.hbs
165
+ ├── login.hbs
166
+ ├── personal.hbs
167
+ ├── users.hbs
168
+ ├── layout
169
+ │ └── default.hbs
170
+ └── partials
171
+ └── .gitkeep
172
+ ```
14
173
 
15
174
  ## Author
16
175
  **Takuya Motoshima**
package/package.json CHANGED
@@ -1,12 +1,11 @@
1
1
  {
2
2
  "name": "express-sweet-generator",
3
- "version": "2.0.2",
4
- "description": "Express Sweet application generator.",
3
+ "version": "2.0.3",
4
+ "description": "EXPRESS SWEET application generator.",
5
5
  "main": "bin/express-sweet-cli.js",
6
6
  "files": [
7
7
  "LICENSE",
8
8
  "README.md",
9
- "QUICK_START.md",
10
9
  "bin/",
11
10
  "templates/",
12
11
  "screencaps/"
Binary file
@@ -1,10 +1,10 @@
1
1
  const express = require('express');
2
- const sweet = require('express-sweet');
2
+ const expressExtension = require('express-sweet');
3
3
 
4
4
  // Creates and configures an ExpressJS web server.
5
5
  const app = express();
6
6
 
7
7
  // Mount the extension.
8
- sweet.mount(app);
8
+ expressExtension.mount(app);
9
9
 
10
10
  module.exports = app;
@@ -66,7 +66,6 @@ module.exports = {
66
66
 
67
67
  /**
68
68
  * URL to redirect after log off, defaults to `/login`.
69
- *
70
69
  * @example
71
70
  * // Set the URL to redirect to in case of login failure as a string.
72
71
  * failure_redirect: '/login',
@@ -1,7 +1,7 @@
1
1
  const path = require('path');
2
2
 
3
3
  /**
4
- * Express Sweet basic configuration interface.
4
+ * EXPRESS SWEET basic configuration interface.
5
5
  */
6
6
  module.exports = {
7
7
  /**
@@ -38,7 +38,6 @@ module.exports = {
38
38
  * This is a hook that rewrites the base URL.
39
39
  * If you want to rewrite the app.locals.baseUrl property and the view's baseUrl variable, use this hook to return a new base URL.
40
40
  * The default value is the referrer's origin (eg https://example.com).
41
- *
42
41
  * @example
43
42
  * rewrite_base_url: baseUrl => {
44
43
  * return `${baseUrl}/admin`;
@@ -54,7 +53,6 @@ module.exports = {
54
53
  * How to determine if it is an ajax request.
55
54
  * The default is that if there is an XMLHttpRequest in the request header (req.xhr) returns true.
56
55
  * For example, if there is no XMLHttpRequest in req(express.Request) and the Ajax endpoint starts with /api, a custom Ajax decision can be made like "return /^\/api\//.test(req.path)".
57
- *
58
56
  * @type {(req: express.Request) => boolean}
59
57
  * @example
60
58
  * is_ajax: req => {
@@ -68,7 +66,6 @@ module.exports = {
68
66
  /**
69
67
  * Hooks the default behavior on request errors.
70
68
  * If unset, simply returns an error HTTP status. (<code>res.status(err.status||500).end();</code>)
71
- *
72
69
  * @type {(err: any, req: express.Request, res: express.Response, next: express.NextFunction) => void}
73
70
  * @example
74
71
  * hook_handle_error: (err, req, res, next) => {
@@ -5,7 +5,7 @@ module.exports = {
5
5
  development: {
6
6
  username: 'root',
7
7
  password: 'password',
8
- database: 'express_sweet_db',
8
+ database: 'sample_db',
9
9
  host: 'localhost',
10
10
  port: undefined,
11
11
  dialect: 'mariadb',
@@ -15,7 +15,7 @@ module.exports = {
15
15
  test: {
16
16
  username: 'root',
17
17
  password: 'password',
18
- database: 'express_sweet_db',
18
+ database: 'sample_db',
19
19
  host: 'localhost',
20
20
  port: undefined,
21
21
  dialect: 'mariadb',
@@ -25,7 +25,7 @@ module.exports = {
25
25
  production: {
26
26
  username: 'root',
27
27
  password: 'password',
28
- database: 'express_sweet_db',
28
+ database: 'sample_db',
29
29
  host: 'localhost',
30
30
  port: undefined,
31
31
  dialect: 'mariadb',
@@ -37,7 +37,6 @@ module.exports = {
37
37
  /**
38
38
  * Hook function just before the view is rendered.
39
39
  * For example, you can set your own local variables that can be used within the view.
40
- *
41
40
  * @example
42
41
  * // The message set here can be referenced in the view as {{message}}.
43
42
  * beforeRender: (req, res) => {
@@ -1,6 +1,6 @@
1
- const Model = require('express-sweet').database.Model;
1
+ const expressExtension = require('express-sweet');
2
2
 
3
- module.exports = class extends Model {
3
+ module.exports = class extends expressExtension.database.Model {
4
4
  static get table() {
5
5
  return 'book';
6
6
  }
@@ -1,6 +1,6 @@
1
- const Model = require('express-sweet').database.Model;
1
+ const expressExtension = require('express-sweet');
2
2
 
3
- module.exports = class extends Model {
3
+ module.exports = class extends expressExtension.database.Model {
4
4
  static get table() {
5
5
  return 'comment';
6
6
  }
@@ -1,6 +1,6 @@
1
- const Model = require('express-sweet').database.Model;
1
+ const expressExtension = require('express-sweet');
2
2
 
3
- module.exports = class extends Model {
3
+ module.exports = class extends expressExtension.database.Model {
4
4
  static get table() {
5
5
  return 'profile';
6
6
  }
@@ -1,8 +1,8 @@
1
+ const expressExtension = require('express-sweet');
1
2
  const {Media} = require('nodejs-shared');
2
- const Model = require('express-sweet').database.Model;
3
3
  const UserNotFound = require('../exceptions/UserNotFound');
4
4
 
5
- module.exports = class extends Model {
5
+ module.exports = class extends expressExtension.database.Model {
6
6
  static get table() {
7
7
  return 'user';
8
8
  }
@@ -10,7 +10,7 @@
10
10
  "dependencies": {
11
11
  "debug": "~2.6.9",
12
12
  "express": "^4.19.2",
13
- "express-sweet": "^2.0.2",
13
+ "express-sweet": "^2.0.3",
14
14
  "express-validator": "^6.14.2",
15
15
  "http-errors": "~1.6.3",
16
16
  "nodejs-shared": "^1.0.28"
@@ -1,11 +1,11 @@
1
1
  const express = require('express');
2
- const Authentication = require('express-sweet').services.Authentication;
2
+ const expressExtension = require('express-sweet');
3
3
  const {query, body, validationResult} = require('express-validator');
4
- const router = express.Router();
5
4
  const UserModel = require('../../models/UserModel');
6
5
  const UserNotFound = require('../../exceptions/UserNotFound');
7
6
  const CustomValidation = require('../../shared/CustomValidation');
8
7
 
8
+ const router = express.Router();
9
9
  router.post('/login', [
10
10
  body('email').trim().not().isEmpty().isEmail(),
11
11
  body('password').trim().not().isEmpty()
@@ -13,12 +13,12 @@ router.post('/login', [
13
13
  const errs = validationResult(req);
14
14
  if (!errs.isEmpty())
15
15
  return void res.status(400).json({errors: errs.array()});
16
- const isAuthenticated = await Authentication.authenticate(req, res, next);
16
+ const isAuthenticated = await expressExtension.services.Authentication.authenticate(req, res, next);
17
17
  res.json(isAuthenticated);
18
18
  });
19
19
 
20
20
  router.get('/logout', (req, res) => {
21
- Authentication.logout(req);
21
+ expressExtension.services.Authentication.logout(req);
22
22
  res.redirect('/');
23
23
  });
24
24
 
@@ -1,6 +1,6 @@
1
1
  const express = require('express');
2
- const router = express.Router();
3
2
 
3
+ const router = express.Router();
4
4
  router.get('/', (req, res) => {
5
5
  res.render('login');
6
6
  });
@@ -1,8 +1,8 @@
1
1
  const express = require('express');
2
- const router = express.Router();
3
2
  const UserModel = require('../models/UserModel');
4
3
 
5
- router.get('/', async (req, res) => {
4
+ const router = express.Router();
5
+ router.get('/', (req, res) => {
6
6
  res.render('users');
7
7
  });
8
8
 
@@ -1,10 +1,10 @@
1
1
  import express from 'express'
2
- import * as sweet from 'express-sweet';
2
+ import * as expressExtension from 'express-sweet';
3
3
 
4
4
  // Creates and configures an ExpressJS web server.
5
5
  const app = express();
6
6
 
7
7
  // Mount the extension.
8
- sweet.mount(app);
8
+ expressExtension.mount(app);
9
9
 
10
10
  export default app;
@@ -4,7 +4,7 @@
4
4
  */
5
5
  import Debug from 'debug';
6
6
  import http from 'http';
7
- import app from '../app';
7
+ import app from '../app.js';
8
8
 
9
9
  /**
10
10
  * Normalize a port into a number, string, or false.
@@ -1,4 +1,4 @@
1
- import UserModel from '../models/UserModel';
1
+ import UserModel from '../models/UserModel.js';
2
2
 
3
3
  /**
4
4
  * User authentication configuration interface.
@@ -66,7 +66,6 @@ export default {
66
66
 
67
67
  /**
68
68
  * URL to redirect after log off, defaults to `/login`.
69
- *
70
69
  * @example
71
70
  * // Set the URL to redirect to in case of login failure as a string.
72
71
  * failure_redirect: '/login',
@@ -1,7 +1,7 @@
1
- import path from 'path';
1
+ import path from 'node:path';
2
2
 
3
3
  /**
4
- * Express Sweet basic configuration interface.
4
+ * EXPRESS SWEET basic configuration interface.
5
5
  */
6
6
  export default {
7
7
  /**
@@ -38,7 +38,6 @@ export default {
38
38
  * This is a hook that rewrites the base URL.
39
39
  * If you want to rewrite the app.locals.baseUrl property and the view's baseUrl variable, use this hook to return a new base URL.
40
40
  * The default value is the referrer's origin (eg https://example.com).
41
- *
42
41
  * @example
43
42
  * rewrite_base_url: baseUrl => {
44
43
  * return `${baseUrl}/admin`;
@@ -54,7 +53,6 @@ export default {
54
53
  * How to determine if it is an ajax request.
55
54
  * The default is that if there is an XMLHttpRequest in the request header (req.xhr) returns true.
56
55
  * For example, if there is no XMLHttpRequest in req(express.Request) and the Ajax endpoint starts with /api, a custom Ajax decision can be made like "return /^\/api\//.test(req.path)".
57
- *
58
56
  * @type {(req: express.Request) => boolean}
59
57
  * @example
60
58
  * is_ajax: req => {
@@ -68,7 +66,6 @@ export default {
68
66
  /**
69
67
  * Hooks the default behavior on request errors.
70
68
  * If unset, simply returns an error HTTP status. (<code>res.status(err.status||500).end();</code>)
71
- *
72
69
  * @type {(err: any, req: express.Request, res: express.Response, next: express.NextFunction) => void}
73
70
  * @example
74
71
  * hook_handle_error: (err, req, res, next) => {
@@ -5,7 +5,7 @@ export default {
5
5
  development: {
6
6
  username: 'root',
7
7
  password: 'password',
8
- database: 'express_sweet_db',
8
+ database: 'sample_db',
9
9
  host: 'localhost',
10
10
  port: undefined,
11
11
  dialect: 'mariadb',
@@ -15,7 +15,7 @@ export default {
15
15
  test: {
16
16
  username: 'root',
17
17
  password: 'password',
18
- database: 'express_sweet_db',
18
+ database: 'sample_db',
19
19
  host: 'localhost',
20
20
  port: undefined,
21
21
  dialect: 'mariadb',
@@ -25,7 +25,7 @@ export default {
25
25
  production: {
26
26
  username: 'root',
27
27
  password: 'password',
28
- database: 'express_sweet_db',
28
+ database: 'sample_db',
29
29
  host: 'localhost',
30
30
  port: undefined,
31
31
  dialect: 'mariadb',
@@ -1,4 +1,4 @@
1
- import path from 'path';
1
+ import path from 'node:path';
2
2
 
3
3
  /**
4
4
  * View's configuration interface.
@@ -37,7 +37,6 @@ export default {
37
37
  /**
38
38
  * Hook function just before the view is rendered.
39
39
  * For example, you can set your own local variables that can be used within the view.
40
- *
41
40
  * @example
42
41
  * // The message set here can be referenced in the view as {{message}}.
43
42
  * beforeRender: (req, res) => {
@@ -5,8 +5,6 @@ module.exports = {
5
5
  {
6
6
  name: '<%- name %>',
7
7
  script: 'bin/www',
8
- node_args: '-r esm-wallaby',
9
- // node_args: '-r esm',
10
8
  exec_mode: 'cluster_mode',
11
9
  watch: '.',
12
10
  watch_delay: 3000,
@@ -1,6 +1,6 @@
1
- import * as sweet from 'express-sweet';
1
+ import * as expressExtension from 'express-sweet';
2
2
 
3
- export default class extends sweet.database.Model {
3
+ export default class extends expressExtension.database.Model {
4
4
  static get table() {
5
5
  return 'book';
6
6
  }
@@ -1,6 +1,6 @@
1
- import * as sweet from 'express-sweet';
1
+ import * as expressExtension from 'express-sweet';
2
2
 
3
- export default class extends sweet.database.Model {
3
+ export default class extends expressExtension.database.Model {
4
4
  static get table() {
5
5
  return 'comment';
6
6
  }
@@ -1,7 +1,7 @@
1
- import * as sweet from 'express-sweet';
2
- import UserModel from './UserModel';
1
+ import * as expressExtension from 'express-sweet';
2
+ import UserModel from './UserModel.js';
3
3
 
4
- export default class extends sweet.database.Model {
4
+ export default class extends expressExtension.database.Model {
5
5
  static get table() {
6
6
  return 'profile';
7
7
  }
@@ -1,11 +1,11 @@
1
+ import * as expressExtension from 'express-sweet';
1
2
  import {Media} from 'nodejs-shared';
2
- import * as sweet from 'express-sweet';
3
- import UserNotFound from '../exceptions/UserNotFound';
4
- import ProfileModel from './ProfileModel';
5
- import CommentModel from './CommentModel';
6
- import BookModel from './BookModel';
3
+ import UserNotFound from '../exceptions/UserNotFound.js';
4
+ import ProfileModel from './ProfileModel.js';
5
+ import CommentModel from './CommentModel.js';
6
+ import BookModel from './BookModel.js';
7
7
 
8
- export default class extends sweet.database.Model {
8
+ export default class extends expressExtension.database.Model {
9
9
  static get table() {
10
10
  return 'user';
11
11
  }
@@ -1,19 +1,17 @@
1
1
  {
2
2
  "name": "<%- name %>",
3
3
  "version": "1.0.0",
4
+ "type": "module",
4
5
  "scripts": {
5
- "start": "nodemon -r esm-wallaby --ignore client/ --ignore public/ --ignore views/ bin/www",
6
- "//start": "nodemon -r esm --ignore client/ --ignore public/ --ignore views/ bin/www",
7
- "////start": "pm2 start ecosystem.config.js",
6
+ "start": "nodemon --ignore client/ --ignore public/ --ignore views/ bin/www",
7
+ "//start": "pm2 start ecosystem.config.js",
8
8
  "//restart": "pm2 restart ecosystem.config.js",
9
9
  "//stop": "pm2 stop ecosystem.config.js"
10
10
  },
11
11
  "dependencies": {
12
12
  "debug": "~2.6.9",
13
- "esm": "^3.2.25",
14
- "esm-wallaby": "^3.2.26",
15
13
  "express": "^4.19.2",
16
- "express-sweet": "^2.0.2",
14
+ "express-sweet": "^2.0.3",
17
15
  "express-validator": "^6.14.2",
18
16
  "http-errors": "~1.6.3",
19
17
  "nodejs-shared": "^1.0.28"
@@ -1,12 +1,11 @@
1
+ import * as expressExtension from 'express-sweet';
1
2
  import {Router} from 'express';
2
- import * as sweet from 'express-sweet';
3
3
  import {query, body, validationResult} from 'express-validator';
4
- import UserModel from '../../models/UserModel';
5
- import UserNotFound from '../../exceptions/UserNotFound';
6
- import * as CustomValidation from '../../shared/CustomValidation';
4
+ import UserModel from '../../models/UserModel.js';
5
+ import UserNotFound from '../../exceptions/UserNotFound.js';
6
+ import * as CustomValidation from '../../shared/CustomValidation.js';
7
7
 
8
8
  const router = Router();
9
- const Authentication = sweet.services.Authentication;
10
9
  router.post('/login', [
11
10
  body('email').trim().not().isEmpty().isEmail(),
12
11
  body('password').trim().not().isEmpty()
@@ -14,12 +13,12 @@ router.post('/login', [
14
13
  const errs = validationResult(req);
15
14
  if (!errs.isEmpty())
16
15
  return void res.status(400).json({errors: errs.array()});
17
- const isAuthenticated = await Authentication.authenticate(req, res, next);
16
+ const isAuthenticated = await expressExtension.services.Authentication.authenticate(req, res, next);
18
17
  res.json(isAuthenticated);
19
18
  });
20
19
 
21
20
  router.get('/logout', (req, res) => {
22
- Authentication.logout(req);
21
+ expressExtension.services.Authentication.logout(req);
23
22
  res.redirect('/');
24
23
  });
25
24
 
@@ -1,6 +1,6 @@
1
1
  import {Router} from 'express';
2
- const router = Router();
3
2
 
3
+ const router = Router();
4
4
  router.get('/', (req, res) => {
5
5
  res.render('login');
6
6
  });
@@ -1,8 +1,8 @@
1
1
  import {Router} from 'express';
2
- import UserModel from '../models/UserModel';
2
+ import UserModel from '../models/UserModel.js';
3
3
 
4
4
  const router = Router();
5
- router.get('/', async (req, res) => {
5
+ router.get('/', (req, res) => {
6
6
  res.render('users');
7
7
  });
8
8
 
@@ -1,4 +1,4 @@
1
- import empty from './empty';
1
+ import empty from './empty.js';
2
2
 
3
3
  /**
4
4
  * Image Data URL validation.
package/QUICK_START.md DELETED
@@ -1,158 +0,0 @@
1
- # Quick Start
2
-
3
- The easiest way to get started with Express Sweet is to install express-sweet-generator and automatically create a template application.
4
- The following application can be started immediately by executing the template.
5
- See <a href="https://takuya-motoshima.github.io/express-sweet/v2/" target="_blank">here</a> for instructions on using express-sweet.
6
-
7
- <p align="center">
8
- <img alt="login" src="screencaps/login.jpg" width="45%">
9
- <img alt="users" src="screencaps/users.jpg" width="45%">
10
- </p>
11
- <p align="center">
12
- <img alt="user-editing" src="screencaps/user-editing.jpg" width="45%">
13
- <img alt="personal" src="screencaps/personal.jpg" width="45%">
14
- </p>
15
-
16
- 1. Use the application generator tool, express-sweet-generator, to quickly create an application skeleton.
17
- ```sh
18
- npm install -g express-sweet-generator
19
- ```
20
-
21
- 1. Create an Express myapp named myapp.
22
- The app is created in a folder named myapp in the working directory.
23
- ```sh
24
- express-sweet myapp
25
- # If you want to use ESM, below.
26
- #express-sweet -o esm myapp
27
- ```
28
- 1. Install dependent packages.
29
- ```sh
30
- cd myapp
31
- npm install
32
- ```
33
- 1. This sample application uses a database.
34
- Execute the following SQL to create the database.
35
- ```sql
36
- CREATE DATABASE IF NOT EXISTS `express_sweet_db` DEFAULT CHARACTER SET utf8mb4;
37
-
38
- USE `express_sweet_db`;
39
-
40
- CREATE TABLE `user` (
41
- `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
42
- `name` varchar(30) NOT NULL,
43
- `email` varchar(255) NOT NULL,
44
- `password` varchar(100) NOT NULL,
45
- `icon` varchar(768) NOT NULL DEFAULT MD5(RAND()),
46
- `created` datetime NOT NULL DEFAULT current_timestamp(),
47
- `modified` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
48
- PRIMARY KEY (`id`),
49
- UNIQUE KEY `ukUserEmail` (`email`),
50
- UNIQUE KEY `ukUserIcon`(`icon`)
51
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
52
-
53
- CREATE TABLE `profile` (
54
- `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
55
- `userId` int(10) unsigned NOT NULL,
56
- `address` varchar(255) NOT NULL,
57
- `tel` varchar(14) NOT NULL,
58
- `created` datetime NOT NULL DEFAULT current_timestamp(),
59
- `modified` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
60
- PRIMARY KEY (`id`),
61
- UNIQUE KEY `ukProfileUserId` (`userId`),
62
- CONSTRAINT `fkProfileUser` FOREIGN KEY (`userId`) REFERENCES `user` (`id`)
63
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
64
-
65
- CREATE TABLE `comment` (
66
- `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
67
- `userId` int(10) unsigned NOT NULL,
68
- `text` text NOT NULL,
69
- `created` datetime NOT NULL DEFAULT current_timestamp(),
70
- `modified` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
71
- PRIMARY KEY (`id`),
72
- CONSTRAINT `fkCommentUser` FOREIGN KEY (`userId`) REFERENCES `user` (`id`)
73
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
74
-
75
- CREATE TABLE `book` (
76
- `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
77
- `userId` int(10) unsigned NOT NULL,
78
- `title` text NOT NULL,
79
- `created` datetime NOT NULL DEFAULT current_timestamp(),
80
- `modified` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
81
- PRIMARY KEY (`id`),
82
- UNIQUE KEY `ukBookTitle` (`userId`, `title`(255)),
83
- CONSTRAINT `fkBookUser` FOREIGN KEY (`userId`) REFERENCES `user` (`id`)
84
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
85
-
86
- INSERT INTO `user` (`id`, `email`, `password`, `name`, `icon`) VALUES
87
- (1, 'robin@example.com', 'password', 'Robin', '/upload/1.png'),
88
- (2, 'taylor@example.com', 'password', 'Taylor', '/upload/2.png');
89
-
90
- INSERT INTO `profile` (`userId`, `address`, `tel`) VALUES
91
- (1, '777 Brockton Avenue, Abington MA 2351', '202-555-0105'),
92
- (2, '30 Memorial Drive, Avon MA 2322', '');
93
-
94
- INSERT INTO `comment` (`userId`, `text`) VALUES
95
- (1, 'From Robin #1'),
96
- (1, 'From Robin #2'),
97
- (2, 'From Taylor #1');
98
-
99
- INSERT INTO `book` (`userId`, `title`) VALUES
100
- (1, 'Beautiful'),
101
- (1, 'Lose Yourself'),
102
- (2, 'When Im Gone');
103
- ```
104
- 1. Build front-end JS.
105
- ```sh
106
- cd myapp/client
107
- npm install
108
- npm run build
109
- ```
110
- 1. Next, you need to set how the database is connected.
111
- The database connection method can be set in config/database.js, so change it according to your environment.
112
- See [here](https://sequelize.org/docs/v6/other-topics/migrations/#cfg) for details.
113
- ```js
114
- // For ESM, use "export default".
115
- //export default {
116
- module.exports = {
117
- development: {
118
- username: 'root',
119
- password: 'password',
120
- database: 'express_sweet_db',
121
- host: 'localhost',
122
- dialect: 'mariadb'
123
- },
124
- test: {
125
- username: 'root',
126
- password: 'password',
127
- database: 'express_sweet_db',
128
- host: 'localhost',
129
- dialect: 'mariadb'
130
- },
131
- production: {
132
- username: 'root',
133
- password: 'password',
134
- database: 'express_sweet_db',
135
- host: 'localhost',
136
- dialect: 'mariadb'
137
- }
138
- }
139
- ```
140
- 1. The DB to be used can be defined individually for each environment.
141
- Specify the environment in the .env file.
142
- ```js
143
- NODE_ENV=development
144
- ```
145
- 1. Launch the application.
146
- ```sh
147
- cd myapp
148
- npm start
149
- ```
150
-
151
- ## express-sweet command options
152
- This generator can also be further configured with the following command line flags.
153
-
154
- --version output the version number
155
- -o, --output <output> add output <module> support (esm|cjs) (defaults to cjs)
156
- -p, --port <port> application listening port (default: 3000)
157
- -f, --force force on non-empty directory
158
- -h, --help output usage information
Binary file
Binary file
Binary file
Binary file