Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PROG2053-Project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Martin Wighus Holtmon
PROG2053-Project
Commits
90b98584
Commit
90b98584
authored
3 years ago
by
Martin Wighus Holtmon
Browse files
Options
Downloads
Patches
Plain Diff
projectSecondPart/Problem2 - Copied over webServer.js from old projectSecondPart.
parent
f6387d49
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
projectSecondPart/webServer.js
+91
-0
91 additions, 0 deletions
projectSecondPart/webServer.js
with
91 additions
and
0 deletions
projectSecondPart/webServer.js
0 → 100644
+
91
−
0
View file @
90b98584
/*
* A simple Node.js program for exporting the current working directory via a webserver listing
* on a hard code (see portno below) port. To start the webserver run the command:
* node webServer.js
*
* Note that anyone able to connect to localhost:3001 will be able to fetch any file accessible
* to the current user in the current directory or any of its children.
*/
/* jshint node: true */
const
express
=
require
(
'
express
'
);
const
portno
=
3000
;
// Port number to use
const
app
=
express
();
const
cs142models
=
require
(
'
./modelData/photoApp.js
'
).
cs142models
;
// We have the express static module (http://expressjs.com/en/starter/static-files.html) do all
// the work for us.
app
.
use
(
express
.
static
(
__dirname
));
app
.
get
(
'
/
'
,
function
(
request
,
response
)
{
response
.
send
(
`Simple web server of files from
${
__dirname
}
`
);
});
app
.
get
(
'
/test/:p1
'
,
function
(
request
,
response
)
{
// Express parses the ":p1" from the URL and returns it in the request.params objects.
const
param
=
request
.
params
.
p1
;
console
.
log
(
'
/test called with param1 =
'
,
param
);
if
(
param
!==
'
info
'
)
{
console
.
error
(
'
Nothing to be done for param:
'
,
param
);
response
.
status
(
400
).
send
(
'
Not found
'
);
return
;
}
const
info
=
cs142models
.
schemaInfo
();
// Query didn't return an error but didn't find the SchemaInfo object - This
// is also an internal error return.
if
(
info
.
length
===
0
)
{
response
.
status
(
500
).
send
(
'
Missing SchemaInfo
'
);
return
;
}
response
.
status
(
200
).
send
(
info
);
});
/*
* URL /user/list - Return all the User object.
*/
app
.
get
(
'
/user/list
'
,
function
(
request
,
response
)
{
response
.
status
(
200
).
send
(
cs142models
.
userListModel
());
return
;
});
/*
* URL /user/:id - Return the information for User (id)
*/
app
.
get
(
'
/user/:id
'
,
function
(
request
,
response
)
{
const
id
=
request
.
params
.
id
;
const
user
=
cs142models
.
userModel
(
id
);
if
(
user
===
null
)
{
console
.
log
(
`User with _id:
${
id
}
not found.`
);
response
.
status
(
400
).
send
(
'
Not found
'
);
return
;
}
response
.
status
(
200
).
send
(
user
);
return
;
});
/*
* URL /photosOfUser/:id - Return the Photos for User (id)
*/
app
.
get
(
'
/photosOfUser/:id
'
,
function
(
request
,
response
)
{
const
id
=
request
.
params
.
id
;
const
photos
=
cs142models
.
photoOfUserModel
(
id
);
if
(
photos
.
length
===
0
)
{
console
.
log
(
`Photos for user with _id:
${
id
}
not found.`
);
response
.
status
(
400
).
send
(
'
Not found
'
);
return
;
}
response
.
status
(
200
).
send
(
photos
);
});
const
server
=
app
.
listen
(
portno
,
function
()
{
const
port
=
server
.
address
().
port
;
console
.
log
(
`Listening at http://localhost:
${
port
}
exporting the directory
${
__dirname
}
`
);
});
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment