Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs=require('fs');
- const mong=require('mongoose');
- const express=require('express');
- const app=express();
- const dotenv=require('dotenv').config();
- const http=require('http');
- const path=require('path')
- const chalk=require('chalk')
- http.createServer((req, res)=>{
- if(req.method=="GET") {
- function isAccess(url){
- let bool=0;
- new Promise((res, rej)=>{
- fs.access(__dirname+url, fs.constants.F_OK, function(err){
- bool= err ? false : true;
- // console.log( bool, url, req.method)
- res(bool)
- })
- }).then(bool=>{
- // console.log('bool'+': '+chalk.red(bool) );
- if(!bool){
- res.writeHead(404, {'Content-type':'text/plain; charset=utf-8'});
- res.end(req.url+" 404 Страница не найдена ")
- }
- })
- }
- isAccess(req.url);
- switch(path.extname(req.url)){
- case "":res.writeHead(200, {"Content-type":"text/html"}); fs.createReadStream(__dirname+"/index.html").pipe(res); break;
- case ".html":res.writeHead(200, {"Content-type":"text/html"}); fs.createReadStream(__dirname+req.url).pipe(res); break;
- case ".css":res.writeHead(200, {"Content-type":"text/css"});fs.createReadStream(__dirname+req.url).pipe(res); break;
- case ".js": res.writeHead(200, {"Content-type":"text/javascript"}); fs.createReadStream(__dirname+req.url).pipe(res); break;
- default :res.writeHead(200, {"Content-type":'text/html; charset=utf-8'});res.end("<h1>404 Страница не найдена h1 </h1>")
- }
- } else{
- let temp='';
- req.on('data', (chunk)=> {
- temp+= chunk;
- // console.log( req.method,req.url, temp )
- });
- req.on('end', () => {
- if(temp){
- try {
- console.log(chalk.green(temp));
- res.write(temp+'1111111');
- res.end(temp+'1111111');
- } catch(e) {
- console.log(e);
- }
- }
- })
- // fs.createReadStream(__dirname+'/package.json').pipe(res);
- }
- })
- .listen(process.env.PORT, ()=>{
- let time=` ${(new Date).getHours()}:${(new Date).getMinutes()}`;
- console.log(`Сервер запустился на порту ${process.env.PORT} в ${chalk.blue.inverse(time)}`);
- })
Advertisement
Add Comment
Please, Sign In to add comment