Casper335
@Casper335
Учусь сам программированию

Как настроить сервер на Node JS?

Здравствуйте, вопрос такой, как подключить код "HTML, CSS, JS" через ссылку на файл ?
const http = require('http');
let html = `
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>Document</title>
		<link rel="stylesheet" href="css/style.css">
	</head>
	<body>
		<h2>Hello World!</h2>
		<script src="js/script.js"></script>
	</body>
</html>
`;

let css = `
	h2{
		font-size: 25px;
		color: blue;
	}
`;

let js = ``;
const server = http.createServer((req, res) => {
	switch(req.url){
		case '/' :
			res.writeHead(200, {'Content-Type': 'text/html'});
			res.end(html);
		break;
		case '/css/style.css' :
			res.writeHead(200, {'Content-Type': 'text/css'});
			res.end(css);
		break;
		case '/js/scrip.js' :
			res.writeHead(200, {'Content-Type': 'text/javascript'});
			res.end(js);
		break;
		default : 
			res.writeHead(200, {'Content-Type': 'text/plain'});
			res.end('404 Не найдено');
		break;
	}
	res.writeHead(200, {'Content-Type': 'text/html'});
	res.end(html);
}).listen(3000, () => console.log('node.js Server started!'));

P.S. Эта тема вообще подойдет для браузерной игры?
Спасибо.))
  • Вопрос задан
  • 76 просмотров
Пригласить эксперта
Ответы на вопрос 1
@L1nks
Попробуй почитать про Express
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы