Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

typescript - http-server is downloading js file instead of displaying the result on the web

I am starting a new typescript project, I have my .ts file, my .js file (compiled from the .ts file), and now I would like to see the result on a web page.

Here is my very simple js file :

var hello = 'hello';

function sayHello(text) {
    return text;
}
sayHello(hello);

I am therefore using http-server (an npm module - https://www.npmjs.com/package/http-server), but when I run the server (via http-server [path] [options] as per the doc) it downloads my file instead of displaying it at the address http://172.17.0.1:8080

No idea what to do now to actually serve and see my file at this address...

The http-server should be dead simple to use.

question from:https://stackoverflow.com/questions/65942810/http-server-is-downloading-js-file-instead-of-displaying-the-result-on-the-web

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is browser can't show JavaScript file as result. You should use in command the folder with index.html. (If you mention index.html, it will also download)

The typical usage is:

http-server

which is equivalent to http-server "./". Comments above mention Content-Type. That's because of HTTP protocol and the fact, the package is not perfect. For you, try to use HTML.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...