Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
nodejs_tutorial
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
jameschiu
nodejs_tutorial
Commits
8126aaa1
Commit
8126aaa1
authored
May 25, 2022
by
james
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[ADD] init
parent
dcdf1ab6
Changes
8
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
820 additions
and
20 deletions
+820
-20
app/config/db.config.js
app/config/db.config.js
+14
-0
app/controllers/user.controller.js
app/controllers/user.controller.js
+140
-0
app/models/index.js
app/models/index.js
+24
-0
app/models/user.model.js
app/models/user.model.js
+19
-0
app/routes/turorial.routes.js
app/routes/turorial.routes.js
+25
-0
index.js
index.js
+26
-18
package-lock.json
package-lock.json
+567
-1
package.json
package.json
+5
-1
No files found.
app/config/db.config.js
0 → 100644
View file @
8126aaa1
module
.
exports
=
{
HOST
:
"
192.168.1.10
"
,
PORT
:
3306
,
USER
:
"
jameschiu
"
,
PASSWORD
:
"
Essen99
"
,
DB
:
"
testDB
"
,
dialect
:
"
mysql
"
,
pool
:
{
max
:
5
,
min
:
0
,
acquire
:
30000
,
idle
:
10000
}
};
app/controllers/user.controller.js
0 → 100644
View file @
8126aaa1
const
db
=
require
(
"
../models
"
);
const
UserModel
=
db
.
users
;
const
Op
=
db
.
Sequelize
.
Op
;
// Create and Save a new User
exports
.
create
=
(
req
,
res
)
=>
{
// Validate request
if
(
!
req
.
body
.
email
)
{
res
.
status
(
400
).
send
({
message
:
"
Content can not be empty!
"
});
return
;
}
// Create a User
const
user
=
{
name
:
req
.
body
.
name
,
ename
:
req
.
body
.
ename
,
email
:
req
.
body
.
email
};
// Save Tutorial in the database
UserModel
.
create
(
user
)
.
then
(
data
=>
{
res
.
send
(
data
);
})
.
catch
(
err
=>
{
res
.
status
(
500
).
send
({
message
:
err
.
message
||
"
Some error occurred while creating the User.
"
});
});
};
// Retrieve all User from the database.
exports
.
findAll
=
(
req
,
res
)
=>
{
const
email
=
req
.
query
.
email
;
var
condition
=
email
?
{
email
:
{
[
Op
.
like
]:
`%
${
email
}
%`
}
}
:
null
;
UserModel
.
findAll
({
where
:
condition
})
.
then
(
data
=>
{
res
.
send
(
data
);
})
.
catch
(
err
=>
{
res
.
status
(
500
).
send
({
message
:
err
.
message
||
"
Some error occurred while retrieving User.
"
});
});
};
// Find a single User with an id
exports
.
findOne
=
(
req
,
res
)
=>
{
const
id
=
req
.
params
.
id
;
UserModel
.
findByPk
(
id
)
.
then
(
data
=>
{
if
(
data
)
{
res
.
send
(
data
);
}
else
{
res
.
status
(
404
).
send
({
message
:
`Cannot find User with id=
${
id
}
.`
});
}
})
.
catch
(
err
=>
{
res
.
status
(
500
).
send
({
message
:
"
Error retrieving User with id=
"
+
id
});
});
};
// Update a User by the id in the request
exports
.
update
=
(
req
,
res
)
=>
{
const
id
=
req
.
params
.
id
;
UserModel
.
update
(
req
.
body
,
{
where
:
{
id
:
id
}
})
.
then
(
num
=>
{
if
(
num
==
1
)
{
res
.
send
({
message
:
"
User was updated successfully.
"
});
}
else
{
res
.
send
({
message
:
`Cannot update User with id=
${
id
}
. Maybe User was not found or req.body is empty!`
});
}
})
.
catch
(
err
=>
{
res
.
status
(
500
).
send
({
message
:
"
Error updating User with id=
"
+
id
});
});
};
// Delete a User with the specified id in the request
exports
.
delete
=
(
req
,
res
)
=>
{
const
id
=
req
.
params
.
id
;
UserModel
.
destroy
({
where
:
{
id
:
id
}
})
.
then
(
num
=>
{
if
(
num
==
1
)
{
res
.
send
({
message
:
"
User was deleted successfully!
"
});
}
else
{
res
.
send
({
message
:
`Cannot delete User with id=
${
id
}
. Maybe User was not found!`
});
}
})
.
catch
(
err
=>
{
res
.
status
(
500
).
send
({
message
:
"
Could not delete User with id=
"
+
id
});
});
};
// Delete all User from the database.
exports
.
deleteAll
=
(
req
,
res
)
=>
{
UserModel
.
destroy
({
where
:
{},
truncate
:
false
})
.
then
(
nums
=>
{
res
.
send
({
message
:
`
${
nums
}
User were deleted successfully!`
});
})
.
catch
(
err
=>
{
res
.
status
(
500
).
send
({
message
:
err
.
message
||
"
Some error occurred while removing all users.
"
});
});
};
app/models/index.js
0 → 100644
View file @
8126aaa1
const
dbConfig
=
require
(
"
../config/db.config.js
"
);
const
Sequelize
=
require
(
"
sequelize
"
);
const
sequelize
=
new
Sequelize
(
dbConfig
.
DB
,
dbConfig
.
USER
,
dbConfig
.
PASSWORD
,
{
logging
:
false
,
host
:
dbConfig
.
HOST
,
dialect
:
dbConfig
.
dialect
,
port
:
dbConfig
.
PORT
,
define
:
{
charset
:
'
utf8
'
,
collate
:
'
utf8_general_ci
'
,
timestamps
:
true
,
},
timezone
:
'
+08:00
'
,
// -->Add this line. for writing to database
});
const
db
=
{};
db
.
Sequelize
=
Sequelize
;
db
.
sequelize
=
sequelize
;
db
.
users
=
require
(
"
./user.model.js
"
)(
sequelize
,
Sequelize
);
module
.
exports
=
db
;
app/models/user.model.js
0 → 100644
View file @
8126aaa1
module
.
exports
=
(
sequelize
,
DataTypes
)
=>
{
const
User
=
sequelize
.
define
(
"
user
"
,
{
id
:
{
type
:
DataTypes
.
INTEGER
,
autoIncrement
:
true
,
allowNull
:
false
,
primaryKey
:
true
},
name
:
DataTypes
.
STRING
,
ename
:
DataTypes
.
STRING
,
email
:
{
type
:
DataTypes
.
STRING
,
allowNull
:
false
,
unique
:
true
,
},
});
return
User
;
};
app/routes/turorial.routes.js
0 → 100644
View file @
8126aaa1
module
.
exports
=
app
=>
{
const
userController
=
require
(
"
../controllers/user.controller.js
"
);
var
router
=
require
(
"
express
"
).
Router
();
// Create a new User
router
.
post
(
"
/
"
,
userController
.
create
);
// Retrieve all Users
router
.
get
(
"
/
"
,
userController
.
findAll
);
// Retrieve a single User with id
router
.
get
(
"
/:id
"
,
userController
.
findOne
);
// Update a User with id
router
.
put
(
"
/:id
"
,
userController
.
update
);
// Delete a User with id
router
.
delete
(
"
/:id
"
,
userController
.
delete
);
// Delete all Users
router
.
delete
(
"
/
"
,
userController
.
deleteAll
);
app
.
use
(
'
/api/user
'
,
router
);
};
index.js
View file @
8126aaa1
const
express
=
require
(
'
express
'
)
const
express
=
require
(
"
express
"
);
const
app
=
express
()
// const bodyParser = require("body-parser"); /* deprecated */
const
port
=
3000
app
.
get
(
'
/
'
,
(
req
,
res
)
=>
{
const
app
=
express
();
res
.
send
(
'
Hello World! (get
'
)
})
app
.
post
(
'
/
'
,
(
req
,
res
)
=>
{
// parse requests of content-type - application/json
res
.
send
(
'
Hello World! (post
'
)
app
.
use
(
express
.
json
());
/* bodyParser.json() is deprecated */
})
app
.
put
(
'
/
'
,
(
req
,
res
)
=>
{
// parse requests of content-type - application/x-www-form-urlencoded
res
.
send
(
'
Hello World! (put
'
)
app
.
use
(
express
.
urlencoded
({
extended
:
true
}));
/* bodyParser.urlencoded() is deprecated */
})
app
.
delete
(
'
/
'
,
(
req
,
res
)
=>
{
const
db
=
require
(
"
./app/models
"
);
res
.
send
(
'
Hello World! (delete
'
)
})
app
.
listen
(
port
,
()
=>
{
db
.
sequelize
.
sync
();
console
.
log
(
`Example app listening on port
${
port
}
`
)
// // drop the table if it already exists
})
// db.sequelize.sync({ force: true }).then(() => {
\ No newline at end of file
// console.log("Drop and re-sync db.");
// });
// simple route
app
.
get
(
"
/
"
,
(
req
,
res
)
=>
{
res
.
json
({
message
:
"
Welcome to bezkoder application.
"
});
});
require
(
"
./app/routes/turorial.routes
"
)(
app
);
// set port, listen for requests
const
PORT
=
process
.
env
.
PORT
||
8080
;
app
.
listen
(
PORT
,
()
=>
{
console
.
log
(
`Server is running on port
${
PORT
}
.`
);
});
\ No newline at end of file
package-lock.json
View file @
8126aaa1
This diff is collapsed.
Click to expand it.
package.json
View file @
8126aaa1
...
@@ -14,6 +14,10 @@
...
@@ -14,6 +14,10 @@
"author"
:
""
,
"author"
:
""
,
"license"
:
"
ISC
"
,
"license"
:
"
ISC
"
,
"dependencies"
:
{
"dependencies"
:
{
"
express
"
:
"
^4.18.1
"
"
express
"
:
"
^4.18.1
"
,
"
body-parser
"
:
"
^1.19.0
"
,
"
cors
"
:
"
^2.8.5
"
,
"
mysql2
"
:
"
^2.0.2
"
,
"
sequelize
"
:
"
^5.21.2
"
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment