Commit ee1dd7bf authored by 14이종민's avatar 14이종민 🥝

express example

parent 7ba7c905
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
# next.js build output
.next
# nuxt.js build output
.nuxt
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
\ No newline at end of file
......@@ -2,6 +2,7 @@
<html>
<head>
<meta charset="utf-8"/>
<script src="/socket.io/socket.io.js"></script>
<script src="script/phaser.js"></script>
<script src="script/Background.js"></script>
<script src="script/Word.js"></script>
......
This diff is collapsed.
{
"name": "sejong25",
"version": "1.0.0",
"description": "",
"main": "run_server.js",
"scripts": {
"test": "test"
},
"repository": {
"type": "git",
"url": "https://git.kucatdog.net/tear-of-sejong/sejong25.git"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"socket.io": "^2.2.0"
}
}
......@@ -34,4 +34,10 @@ function create()
function update()
{
}
\ No newline at end of file
}
var socket = io.connect();
socket.on('hi', function(msg) {
console.log(msg);
});
socket.emit('hello');
var http = require("http");
var fs = require("fs");
var url = require("url");
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io').listen(server);
http.createServer(function(request, response) {
// URL 뒤에 있는 디렉토리/파일이름 파싱
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
// 파일 이름이 비어있다면 index.html 로 설정
if(pathname=="/"){
pathname = "/index.html";
}
// 파일을 읽기
fs.readFile(pathname.substr(1), function (err, data) {
if (err) {
console.log(err);
// 페이지를 찾을 수 없음
// HTTP Status: 404 : NOT FOUND
// Content Type: text/plain
response.writeHead(404, {'Content-Type': 'text/html'});
}else{
// 페이지를 찾음
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/html'});
// 파일을 읽어와서 responseBody 에 작성
response.write(data.toString());
}
// responseBody 전송
response.end();
});
}).listen(8081);
app.use('/css', express.static(__dirname + '/css'));
app.use('/script', express.static(__dirname + '/script'));
app.use('/assets', express.static(__dirname + '/assets'));
console.log('Server running at http://127.0.0.1:8081/');
\ No newline at end of file
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
// http 기본 포트(80)에 서버 열기
server.listen(80, function() {
console.log('Listening on port ' + server.address().port);
});
// 클라이언트 요청에 대한 콜백 정의
io.on('connection', function(socket) {
socket.on('hello', function() {
console.log('client request');
socket.emit('hi', 'Hello, Client!');
});
});
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment