Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// configuration property to enable it.
Log.init({
writers: [{
name: "my-writer",
type: "my-example-writer"
}]
});
// Then everything is exactly the same as basic usage.
//
// Start the log...
Log.start();
// Log something out to it...
// See the MyExampleCustomWriter.js file for implementation details.
Log.info("This is an example log message.");
// stop when we are done.
Log.stop();
name: "console",
type: "default",
levels: "*",
formatter: "my-example-formatter",
options: {}
}]
});
// Then everything is exactly the same as basic usage.
//
// Start the log...
Log.start();
// Log something out to it...
// See the MyExampleCustomWriter.js file for implementation details.
Log.info("This is an example log message.");
// stop when we are done.
Log.stop();
&& (!handler || route.handler===handler);
});
// remove them
this[$ROUTES] = this[$ROUTES].filter((route)=>{
return matching.indexOf(route)<0;
});
// cleanup child routes if any
matching.forEach((route)=>{
(route.children||[]).forEach((child)=>{
_unroute.call(this,child.method,child.path,child.handler);
});
});
if (matching.length>0 && this.config.informative) Log.info("Removed route "+method+" "+path.toString());
return matching.length>0;
};
server.close((err)=>{
if (err) {
Log.error("Error stopping HTTPS server on "+hostname+":"+port+".",err);
this[$RUNNING] = false;
this[$SERVER] = null;
reject(err);
}
else {
if (this.config.informative) Log.info("Stopped HTTPS Server on "+hostname+":"+port+"...");
this[$RUNNING] = false;
this[$SERVER] = null;
resolve();
}
});
}
if (this.config.informative) Log.info("Starting...");
if (this[$SERVERS].size<1) {
Log.warn("No servers defined. Nothing to do.");
}
else {
await Promise.all([...this[$SERVERS]].map((server)=>{
let prom = server.start(this.handler.bind(this));
if (prom instanceof Promise) return prom;
return Promise.resolve();
}));
}
this[$RUNNING] = true;
if (this.config.informative) Log.info("Started.");
}
async any(path,request,response) {
Log.info("Redirecting "+(this.temporary?"temporarilly":"permanently")+" to "+this.toPath);
response.writeHead(this.temporary ? 302 : 301,{
Location: this.toPath
});
await response.end();
}
}
stop() {
if (!this[$SERVER]) return Promise.resolve();
let hostname = this.hostname || "localhost";
let port = this.port || 0;
if (this.config.informative) Log.info("Stopping HTTP/2 Server on "+hostname+":"+port+"...");
return new Promise(async (resolve,reject)=>{
try {
let server = this[$SERVER];
await Promise.all(this[$SESSIONS].map((session)=>{
return new Promise((resolve,reject)=>{
try {
session.close(()=>{
resolve();
});
}
catch (ex) {
return reject(ex);
}
});
}));
_routeFile.call(this,route,filename,additionalArgs);
}
else {
throw new Error("Invalid handler, is neither directory or file: "+filename);
}
}
if (!route.routes) throw new Error("Invalid handler.");
this.unroute(method,path,handler);
this[$ROUTES].push(route);
if (parent) {
parent.children = parent.children || [];
parent.children.push(route);
}
if (this.config.informative) Log.info("Added route "+method+" "+route.matcher.toString());
return route;
};
stop() {
if (!this[$SERVER]) return Promise.resolve();
let hostname = this.hostname || "localhost";
let port = this.port || 0;
if (this.config.informative) Log.info("Stopping HTTP Server on "+hostname+":"+port+"...");
return new Promise((resolve,reject)=>{
try {
let server = this[$SERVER];
server.close((err)=>{
if (err) {
Log.error("Error stopping HTTP server on "+hostname+":"+port+".",err);
this[$RUNNING] = false;
this[$SERVER] = null;
reject(err);
}
else {
if (this.config.informative) Log.info("Stopped HTTP Server on "+hostname+":"+port+"...");
this[$RUNNING] = false;
this[$SERVER] = null;
resolve();
}
async start() {
if (this.running) return Promise.resolve();
if (this.config.informative) Log.info("Starting...");
if (this[$SERVERS].size<1) {
Log.warn("No servers defined. Nothing to do.");
}
else {
await Promise.all([...this[$SERVERS]].map((server)=>{
let prom = server.start(this.handler.bind(this));
if (prom instanceof Promise) return prom;
return Promise.resolve();
}));
}
this[$RUNNING] = true;
if (this.config.informative) Log.info("Started.");
}