Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
M
mobile wireless
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
KANTAPONG SONG-NGAM
mobile wireless
Commits
b5b120bb
Commit
b5b120bb
authored
Oct 28, 2018
by
KANTAPONG SONG-NGAM
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
homework
parent
d06a6726
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
49 additions
and
49 deletions
+49
-49
package-lock.json
mobile/package-lock.json
+0
-0
package.json
mobile/package.json
+2
-1
Things.js
mobile/week01/Things.js
+0
-1
Users.js
mobile/week01/Users.js
+0
-0
testmongoose.js
mobile/week01/testmongoose.js
+0
-0
config.js
mobile/week02/config.js
+0
-0
server.js
mobile/week02/server.js
+47
-47
user.js
mobile/week02/user.js
+0
-0
No files found.
mobile/package-lock.json
View file @
b5b120bb
This diff is collapsed.
Click to expand it.
mobile/package.json
View file @
b5b120bb
...
@@ -9,10 +9,11 @@
...
@@ -9,10 +9,11 @@
"author"
:
""
,
"author"
:
""
,
"license"
:
"ISC"
,
"license"
:
"ISC"
,
"dependencies"
:
{
"dependencies"
:
{
"bcrypt"
:
"^3.0.2"
,
"body-parser"
:
"^1.18.3"
,
"body-parser"
:
"^1.18.3"
,
"express"
:
"^4.16.4"
,
"express"
:
"^4.16.4"
,
"jsonwebtoken"
:
"^8.3.0"
,
"jsonwebtoken"
:
"^8.3.0"
,
"mongoose"
:
"^5.3.
4
"
,
"mongoose"
:
"^5.3.
7
"
,
"morgan"
:
"^1.9.1"
"morgan"
:
"^1.9.1"
}
}
}
}
mobile/Things.js
→
mobile/
week01/
Things.js
View file @
b5b120bb
...
@@ -20,4 +20,3 @@ ThingSchema = new Schema({
...
@@ -20,4 +20,3 @@ ThingSchema = new Schema({
}
}
})
})
module
.
exports
=
mongoose
.
model
(
'Things'
,
ThingSchema
)
module
.
exports
=
mongoose
.
model
(
'Things'
,
ThingSchema
)
mobile/Users.js
→
mobile/
week01/
Users.js
View file @
b5b120bb
File moved
mobile/testmongoose.js
→
mobile/
week01/
testmongoose.js
View file @
b5b120bb
File moved
mobile/config.js
→
mobile/
week02/
config.js
View file @
b5b120bb
File moved
mobile/server.js
→
mobile/
week02/
server.js
View file @
b5b120bb
...
@@ -2,8 +2,9 @@ express = require('express');
...
@@ -2,8 +2,9 @@ express = require('express');
app
=
express
();
app
=
express
();
bodyParser
=
require
(
'body-parser'
);
bodyParser
=
require
(
'body-parser'
);
morgan
=
require
(
'morgan'
);
morgan
=
require
(
'morgan'
);
mongoose
=
require
(
'mongoose'
);
mongoose
=
require
(
'mongoose'
);
jwt
=
require
(
'jsonwebtoken'
);
jwt
=
require
(
'jsonwebtoken'
);
config
=
require
(
'./config'
);
config
=
require
(
'./config'
);
User
=
require
(
'./user'
);
User
=
require
(
'./user'
);
...
@@ -11,7 +12,7 @@ User = require('./user');
...
@@ -11,7 +12,7 @@ User = require('./user');
// configuration
// configuration
// =======================
// =======================
// server setting
// server setting
var
port
=
process
.
env
.
PORT
||
8000
;
port
=
process
.
env
.
PORT
||
8000
;
// connect databse
// connect databse
mongoose
.
connect
(
config
.
database
,{
useNewUrlParser
:
true
});
mongoose
.
connect
(
config
.
database
,{
useNewUrlParser
:
true
});
...
@@ -24,7 +25,7 @@ app.use(bodyParser.urlencoded({ extended: false}));
...
@@ -24,7 +25,7 @@ app.use(bodyParser.urlencoded({ extended: false}));
app
.
use
(
bodyParser
.
json
());
app
.
use
(
bodyParser
.
json
());
// log request
// log request
app
.
use
(
morgan
(
'
dev
'
));
app
.
use
(
morgan
(
'
combined
'
));
// =======================
// =======================
// routes
// routes
...
@@ -49,16 +50,45 @@ app.get('/setup', function(req, res) {
...
@@ -49,16 +50,45 @@ app.get('/setup', function(req, res) {
// API ROUTES ================
// API ROUTES ================
apiRoutes
=
express
.
Router
();
apiRoutes
=
express
.
Router
();
// GET(http://localhost:8080/api/)
apiRoutes
.
get
(
'/'
,
function
(
req
,
res
)
{
res
.
json
({
message
:
'Welcome to API routing'
});
});
// POST(http://localhost:8000/api/authenticate)
apiRoutes
.
post
(
'/authenticate'
,
function
(
req
,
res
)
{
// find db by posted name
User
.
findOne
({
username
:
req
.
body
.
username
},
function
(
err
,
user
)
{
if
(
err
)
throw
err
;
// validation
if
(
!
user
)
{
res
.
json
({
success
:
false
,
message
:
'Authentication failed. User not found.'
});
return
;
}
if
(
user
.
password
!=
req
.
body
.
password
)
{
res
.
json
({
success
:
false
,
message
:
'Authentication failed. Wrong password.'
});
return
;
}
// when valid -> create token
var
token
=
jwt
.
sign
(
user
.
toJSON
(),
app
.
get
(
'superSecret'
),
{
expiresIn
:
'24h'
});
res
.
json
({
success
:
true
,
message
:
'Authentication successfully finished.'
,
token
:
token
});
});
});
// Authentification Filter
// Authentification Filter
apiRoutes
.
use
(
function
(
req
,
res
,
next
)
{
apiRoutes
.
use
(
function
(
req
,
res
,
next
)
{
// get token from body:token or query:token of Http Header:x-access-token
// get token from body:token or query:token of Http Header:x-access-token
var
token
=
req
.
body
.
token
||
req
.
query
.
token
||
req
.
headers
[
'x-access-token'
];
token
=
req
.
body
.
token
||
req
.
query
.
token
||
req
.
headers
[
'x-access-token'
];
// validate token
// validate token
if
(
!
token
)
{
if
(
!
token
)
{
...
@@ -80,58 +110,27 @@ apiRoutes.use(function(req, res, next) {
...
@@ -80,58 +110,27 @@ apiRoutes.use(function(req, res, next) {
});
});
});
});
// GET(http://localhost:8000/api/)
apiRoutes
.
get
(
'/'
,
function
(
req
,
res
)
{
res
.
json
({
message
:
'Welcome to API routing'
});
});
// GET(http://localhost:80
8
0/api/users)
// GET(http://localhost:80
0
0/api/users)
apiRoutes
.
get
(
'/users'
,
function
(
req
,
res
)
{
apiRoutes
.
get
(
'/users'
,
function
(
req
,
res
)
{
User
.
find
({},
function
(
err
,
users
)
{
User
.
find
({},
function
(
err
,
users
)
{
if
(
err
)
throw
err
;
if
(
err
)
throw
err
;
res
.
json
(
users
);
res
.
json
(
users
);
});
});
});
});
// apply the routes to our application(prefix /api)
// apply the routes to our application(prefix /api)
app
.
use
(
'/api'
,
apiRoutes
);
app
.
use
(
'/api'
,
apiRoutes
);
// POST(http://localhost:8080/api/authenticate)
apiRoutes
.
post
(
'/authenticate'
,
function
(
req
,
res
)
{
// find db by posted name
User
.
findOne
({
username
:
req
.
body
.
username
},
function
(
err
,
user
)
{
if
(
err
)
throw
err
;
// validation
if
(
!
user
)
{
res
.
json
({
success
:
false
,
message
:
'Authentication failed. User not found.'
});
return
;
}
if
(
user
.
password
!=
req
.
body
.
password
)
{
res
.
json
({
success
:
false
,
message
:
'Authentication failed. Wrong password.'
});
return
;
}
// when valid -> create token
var
token
=
jwt
.
sign
(
user
.
toJSON
(),
app
.
get
(
'superSecret'
),
{
expiresIn
:
'24h'
});
res
.
json
({
success
:
true
,
message
:
'Authentication successfully finished.'
,
token
:
token
});
});
});
// =======================
// =======================
// start the server
// start the server
// =======================
// =======================
app
.
listen
(
port
);
app
.
listen
(
port
);
console
.
log
(
'started http://localhost:'
+
port
+
'/'
);
console
.
log
(
'started http://localhost:'
+
port
+
'/'
);
\ No newline at end of file
mobile/user.js
→
mobile/
week02/
user.js
View file @
b5b120bb
File moved
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