2017-04-12 3 views
1

나는 익스프레스를 배우려고 노력하고있다. 내 HTML 페이지를로드하는 데 FS를 사용하고 있습니다. Google 검색을 통해 Express에서 대신 ASP.NET을 사용하고 있다는 것을 알 수있었습니다.Express 전용 메인 페이지 로딩

Server.js :

var express = require('express'); 
var path = require('path'); 
var fs = require('fs'); 

var app = express(); 

app.use(require('body-parser').urlencoded({ extended: true })); 
app.use(express.static(path.join(__dirname+'/'))) 

app.get('/', function(a,b,c){ 
    fs.readFileSync('index.html'); 
}); 

app.post('/testaction', function(a,b){ 
    console.log(a.body.log); 
    fs.readFileSync('Logged.html'); 
}); 

app.listen(3000); 

index.html을 : 당신이 FS를 사용하지 않아도 파일을 제공하기 위해

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Test Page</title> 
    </head> 
    <body> 
    <form method="post" action="http://localhost:3000/testaction"> 
     <h1>Log This:</h1> 
     <input type="text" name="log"/> 
     <input type="submit", value="Press Me!"/> 
    </form> 
    </body> 
</html> 

답변

1

. 예 : 리디렉션해야하는 경우

app.get('/', function(req, res, next) { 
    res.render('index'); 
}); 

, 당신은 사용할 수 있습니다

app.get('/', function(a,b,c){ 
    fs.readFileSync('index.html'); 
}); 

이 줄은 미들웨어이며 모든 요청에 ​​모든 경로에 적용

res.redirect('/staticFile.html'); 
0

다음 줄을 제거 :

app.use(express.static(path.join(__dirname+'/'))) 

당신이 제공하는 경로에서 정적 파일을 제공하는 것이 그 목적입니다. 이 경우 루트 디렉토리에서 서비스하므로 예를 들어 http://localhost:3000/package.json과 같은 모든 코드 파일을 탐색 할 수 있습니다.

그것은 당신이 app.get('/'을 쓸 때 당신은 새로운 경로와 미들웨어를 무시하고 하나의 파일 만 제공하려고

... 게시물 등을 넣어 얻을, 모든 HTTP 메소드에 적용됩니다. 이 코드를 삭제하면 위에서 지정한 디렉토리에서 모든 것을 정적으로 서버에 배치 할 수 있습니다.

app.use('/', express.static(__dirname + '/site')); 
: 당신은 당신의 코드 파일을 제공하지 않으려면

는, 예를 들면, "사이트"와 같은 다른 폴더에 정적 파일을 넣고 그 폴더에 정적 경로를 설정