Skip to content
Snippets Groups Projects
Commit e6957e27 authored by Mamadu-lamarana Bah's avatar Mamadu-lamarana Bah :speech_balloon:
Browse files

updt projet

parent b0c527a0
Branches
No related tags found
No related merge requests found
Showing
with 309 additions and 199 deletions
import defaultBuilder from './defaultBuilder.js';
import aboutBuilder from './aboutBuilder.js';
import auctioneerBuilder from './auctioneerBuilder.js'
import { URL } from 'url';
export default class ResponseBuilder {
request;
response;
url;
constructor(request, response) {
this.request =request,
this.response =response;
this.url = new URL(request.url, `http://${request.headers.host}`); }
handleResponse() {
this.prepareResponse();
this.buildResponse();
}
prepareResponse() {
this.response.statusCode = 200;
this.response.setHeader( 'Content-Type' , 'text/html');
}
buildResponse() {
// routage "à la main"
if (this.url.pathname.startsWith('/about')){
const about = new aboutBuilder(this.request, this.response);
about.bresponse();
about.endResponse();
}
else if(this.url.pathname.startsWith('/auctioneer')) {
const auctioneer = new auctioneerBuilder(this.request, this.response);
auctioneer.bresponse();
auctioneer.endResponse();
}
else if (this.url.pathname.startsWith('/')){
const def = new defaultBuilder(this.request, this.response);
def.bresponse();
def.endResponse();
}
else {
new ErrorResponseBuilder(this.request, this.response, 404).handleError();
}
}
}
\ No newline at end of file
import HtmlResponseBuilder from "./HtmlResponseBuilder.js";
export default class aboutBuilder extends HtmlResponseBuilder{
constructor(request, response){
super(request, response);
}
bresponse() {
this.response.write(`<html>
<head>
<meta charset="UTF-8">
<title>Welcome to second node server</title>
<h1>Node server</h1>
</head>
<body>
<p>Welcome to second trivial server</p>
</body>`);
}
}
\ No newline at end of file
import HtmlResponseBuilder from "./HtmlResponseBuilder.js";
export default class auctioneerBuilder extends HtmlResponseBuilder{
constructor(request, response){
super(request, response);
}
bresponse() {
this.response.write(`<html>
<head>
<meta charset="UTF-8">
<title>Welcome to second node server</title>
<h1>Node server</h1>
</head>
<body>
<p>Welcome to second trivial server</p>
</body>`);
}
}
\ No newline at end of file
import HtmlResponseBuilder from "./HtmlResponseBuilder.js";
export default class defaultBuilder extends HtmlResponseBuilder{
constructor(request, response){
super(request, response);
}
bresponse() {
this.response.write(`<html>
<head>
<meta charset="UTF-8">
<title>Welcome to first node server </title>
<link href="./public/style/style.css" rel="" type="text/css">
<h1>Node server</h1>
</head>
<body>
<p class="ok">Welcome server</p>
<ul>
<li> <a href="http://${this.request.headers.host}/about">about</a> </li>
<li> <a href="http://${this.request.headers.host}/auctioneer">auctioneer</a> </li>
<li> <a href="http://${this.request.headers.host}/bidder">bidder</a> </li>
</ul>
`);
}
}
\ No newline at end of file
export default class ErrorResponseBuilder {
#response;
#errorStatus;
#url;
constructor(url, response, errorStatus) {
this.#response = response;
this.#errorStatus = errorStatus;
this.#url = url;
}
handleError() {
this.prepareResponse();
this.bResponse();
}
prepareResponse() {
this.#response.statusCode = this.#errorStatus;
this.#response.setHeader( 'Content-Type' , 'text/html');
}
bResponse() {
this.#response.write(`${this.#errorStatus} : ${this.#url} not found`);
this.#response.end();
}
}
\ No newline at end of file
export default class HtmlResponseBuilder { export default class HtmlResponseBuilder {
request;
response; response;
url; url;
constructor(request, response) { constructor(url, response) {
this.request =request, this.url = url;
this.response =response; this.response =response;
} }
endResponse() {
this.response.write(`<footer>${Date()}</footer>`);
this.response.write(`</body> </html>`);
this.response.end();
}
} }
\ No newline at end of file
import { URL } from 'url'; import { URL } from 'url';
import ResponseBuilder from './ResponseBuilder.js' import ResponseBuilder from './ResponseBuilder.js'
import { getContentTypeFrom } from '../scripts/contentTypeUtil.js';
const BASE = 'http://localhost/';
export default class RequestController { export default class RequestController {
...@@ -10,19 +13,27 @@ export default class RequestController { ...@@ -10,19 +13,27 @@ export default class RequestController {
constructor(request, response) { constructor(request, response) {
this.#request = request, this.#request = request,
this.#response = response; this.#response = response;
this.#url = new URL(request.url, `http://${request.headers.host}`); this.#url = new URL(this.request.url,BASE).pathname; // on ne considère que le "pathname" de l'URL de la requête
} }
get response() { get response() {
return this.#response; return this.#response;
} }
get request() {
return this.#request;
}
get url() {
return this.#url;
}
handleRequest() { async handleRequest() {
this.buildResponse(); this.response.setHeader("Content-Type" , getContentTypeFrom(this.url) || 'text/html');
await this.buildResponse();
this.response.end();
} }
buildResponse() { buildResponse() {
new ResponseBuilder(this.#request, this.#response).handleResponse(); new ResponseBuilder(this.url, this.#response).handleResponse();
} }
} }
\ No newline at end of file
import defaultBuilder from './default.js';
import aboutBuilder from './about.js';
import auctioneerBuilder from './auctioneer.js'
import ErrorResponseBuilder from './ErrorResponseBuilder.js';
import bidderBuilder from './bidder.js';
import { URL } from 'url';
export default class ResponseBuilder {
#request;
#response;
#url;
constructor(url, response) {
this.#response =response;
this.#url = url;
}
get url() {
return this.#url;
}
get response() {
return this.#response;
}
get request() {
return this.#request;
}
handleResponse() {
this.buildResponse();
}
buildResponse() {
// routage "à la main"
if (this.url === '/' ){
new defaultBuilder(this.url, this.response).buildResponse();
}
else if (this.url.startsWith('/about')){
new aboutBuilder(this.url, this.response).buildResponse();
}
else if(this.url.startsWith('/auctioneer')) {
new auctioneerBuilder(this.url, this.response).buildResponse();
}
else if(this.url.startsWith('/bidder')) {
new bidderBuilder(this.url, this.response).buildResponse();
}
else {
new ErrorResponseBuilder(this.url, this.response, 404).handleError();
}
}
}
\ No newline at end of file
import HtmlResponseBuilder from "./HtmlResponseBuilder.js";
import * as fs from 'fs';
export default class about extends HtmlResponseBuilder{
constructor(url, response){
super(url, response);
}
buildResponse() {
let path = `..${this.url}.html`;
try {
fs.accessSync(path, fs.constants.R_OK);
const content = fs.readFileSync(path);
this.response.write(content);
this.response.end();
}catch(err) {
new ErrorResponseBuilder(this.url, this.response, 404).handleError();
}
}
}
\ No newline at end of file
import HtmlResponseBuilder from "./HtmlResponseBuilder.js";
import * as fs from 'fs';
export default class auctioneer extends HtmlResponseBuilder{
constructor(url, response){
super(url, response);
}
buildResponse() {
let path = `..${this.url}.html`;
try {
fs.accessSync(path, fs.constants.R_OK);
const content = fs.readFileSync(path);
this.response.write(content);
}catch(err) {
new ErrorResponseBuilder(this.url, this.response, 404).handleError();
}
}
}
\ No newline at end of file
import HtmlResponseBuilder from "./HtmlResponseBuilder.js";
import * as fs from 'fs';
export default class bidder extends HtmlResponseBuilder{
constructor(url, response){
super(url, response);
}
buildResponse() {
let path = `..${this.url}.html`;
try {
fs.accessSync(path, fs.constants.R_OK);
const content = fs.readFileSync(path);
this.response.write(content);
}catch(err) {
new ErrorResponseBuilder(this.url, this.response, 404).handleError();
}
}
}
\ No newline at end of file
import HtmlResponseBuilder from "./HtmlResponseBuilder.js";
import * as fs from 'fs';
import ErrorResponseBuilder from "./ErrorResponseBuilder.js";
export default class defaultBuilder extends HtmlResponseBuilder{
constructor(url, response){
super(url, response);
}
/**
* send the requested resource as it is, if it exists, else responds with a 404
*/
buildResponse() {
const path = `..${this.url}index.html`;
try {
console.log(path);
fs.accessSync(path, fs.constants.R_OK);
const content = fs.readFileSync(path);
this.response.write(content);
}catch(err) {
new ErrorResponseBuilder(this.url, this.response, 404).handleError();
}
}
}
\ No newline at end of file
export default class IOController {
#io;
#clients;
#interval = 2000;
#maxRandomNumber = 8;
constructor(io) {
this.#io = io;
this.#clients = new Map();
}
registerSocket(socket) {
console.log(`new connection with id ${socket.id}`);
socket.emit('ping', 'ping');
this.setupListeners(socket);
}
setupListeners(socket) {
socket.on( 'pong', () => this.greatings(socket) );
socket.on( 'disconnect' , () => this.leave(socket) );
}
greatings(socket) {
console.log(`pong received from (id : ${socket.id}) at ${new Date().toLocaleTimeString()}`);
//const timerId = setInterval(this.randomNumber.bind(this), this.#interval);
//const timerId = setInterval(this.randomNumber.bind(this), this.#interval, socket); /**si on veut que le nombre random soit différent */
this.#clients.set(socket.id, timerId);
}
leave(socket) {
const timerId = this.#clients.get(socket.id) || 'unknown' ;
console.log(`disconnection from ${socket.id}`);
clearInterval(timerId);
this.#clients.delete(socket.id);
}
randomNumber(socket=undefined) {
const number = randInt(this.#maxRandomNumber);
if (socket) {
socket.emit('change number', number);
}
else{
this.#io.emit('change number', number);
}
console.log(`nombre envoyé ${number}`);
}
}
const randInt = max => Math.floor( Math.random() * max );
\ No newline at end of file
import http from 'http'; import http from 'http';
import RequestController from './controller/RequestController.js'; import RequestController from './controllers/RequestController.js';
import { Server as ServerIO } from 'socket.io';
import IOController from './controllers/ioController.js';
const server = http.createServer( const server = http.createServer(
(request, response) => new RequestController(request, response).handleRequest() (request, response) => new RequestController(request, response).handleRequest()
); );
server.listen(8080); server.listen(8080);
\ No newline at end of file
<!DOCTYPE html> <head><script defer src="scripts/bundle.js?50d73e68ded995014797"></script></head>
\ No newline at end of file
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
<meta charset="UTF-8"/>
<title>PAGE TITLE</title>
<meta name="author" content="your name"/>
<link rel="stylesheet" type="text/css" href="./style/style.css" title="starship style"/>
<script defer src="scripts/bundle.js?298a7feaf160465c83e4"></script></head>
<body>
<h1>Your page</h1>
<p>Write your html code here.</p>
<p>Consultez la console pour la trace de création du bundle&nbsp;: <kbd>Ctrl+Shift+K</kbd></p>
</body>
</html>
This diff is collapsed.
/*!*****************************!*\ /*!*****************************!*\
!*** ./src/scripts/main.js ***! !*** ./src/scripts/main.js ***!
\*****************************/ \*****************************/
/*!*******************************************!*\
!*** ./src/controller/ResponseBuilder.js ***!
\*******************************************/
/*!*********************************************!*\
!*** ./src/controller/RequestController.js ***!
\*********************************************/
/*!***********************************************************!*\
!*** ./node_modules/@babel/runtime/helpers/esm/typeof.js ***!
\***********************************************************/
/*!****************************************************************!*\
!*** ./node_modules/@babel/runtime/helpers/esm/createClass.js ***!
\****************************************************************/
/*!****************************************************************!*\
!*** ./node_modules/@babel/runtime/helpers/esm/toPrimitive.js ***!
\****************************************************************/
/*!******************************************************************!*\
!*** ./node_modules/@babel/runtime/helpers/esm/toPropertyKey.js ***!
\******************************************************************/
/*!*******************************************************************!*\
!*** ./node_modules/@babel/runtime/helpers/esm/classCallCheck.js ***!
\*******************************************************************/
/*!*******************************************************************!*\
!*** ./node_modules/@babel/runtime/helpers/esm/defineProperty.js ***!
\*******************************************************************/
/*!*************************************************************************!*\
!*** ./node_modules/@babel/runtime/helpers/esm/classPrivateFieldGet.js ***!
\*************************************************************************/
/*!*************************************************************************!*\
!*** ./node_modules/@babel/runtime/helpers/esm/classPrivateFieldSet.js ***!
\*************************************************************************/
/*!****************************************************************************!*\
!*** ./node_modules/@babel/runtime/helpers/esm/classApplyDescriptorGet.js ***!
\****************************************************************************/
/*!****************************************************************************!*\
!*** ./node_modules/@babel/runtime/helpers/esm/classApplyDescriptorSet.js ***!
\****************************************************************************/
/*!********************************************************************************!*\
!*** ./node_modules/@babel/runtime/helpers/esm/classExtractFieldDescriptor.js ***!
\********************************************************************************/
/**
return a content-type deduced from <path> final extension
@param path the path where to look for final extension
*/
export const getContentTypeFrom = path => {
const lastPointPosition = path.lastIndexOf('.');
const extension = path.substring(lastPointPosition);
return contentTypes.get(extension) || '';
}
// the map that associates extenstion to content-type
const contentTypes = new Map().set('.css', 'text/css')
.set('.html', 'text/html')
.set('.jpg',"image/jpeg")
.set('.jpeg',"image/jpeg")
.set('.txt',"plain/text")
.set('.png',"image/png")
.set('.js', 'application/javascript')
.set('.json', 'application/json');
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment