Using node.js as a simple web server

后端 未结 30 2548
感情败类
感情败类 2020-11-22 02:54

I want to run a very simple HTTP server. Every GET request to example.com should get index.html served to it but as a regular HTML page (i.e., same

30条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 03:28

    Crazy amount of complicated answers here. If you don't intend to process nodeJS files/database but just want to serve static html/css/js/images as your question suggest then simply install the pushstate-server module or similar;

    Here's a "one liner" that will create and launch a mini site. Simply paste that entire block in your terminal in the appropriate directory.

    mkdir mysite; \
    cd mysite; \
    npm install pushstate-server --save; \
    mkdir app; \
    touch app/index.html; \
    echo '

    Hello World

    ' > app/index.html; \ touch server.js; \ echo "var server = require('pushstate-server');server.start({ port: 3000, directory: './app' });" > server.js; \ node server.js

    Open browser and go to http://localhost:3000. Done.

    The server will use the app dir as the root to serve files from. To add additional assets just place them inside that directory.

提交回复
热议问题