How to publish a website made by Node.js to Github Pages?

前端 未结 5 1622
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 16:43

I made a website using Node.js as the server. As I know, the node.js file should start working by typing commands in terminal, so I\'m not sure if Github Pages supports node

相关标签:
5条回答
  • 2020-11-29 16:45

    GitHub pages host only static HTML pages. No server side technology is supported, so Node.js applications won't run on GitHub pages. There are lots of hosting providers, as listed on the Node.js wiki.

    App fog seems to be the most economical as it provides free hosting for projects with 2GB of RAM (which is pretty good if you ask me).
    As stated here, AppFog removed their free plan for new users.

    If you want to host static pages on GitHub, then read this guide. If you plan on using Jekyll, then this guide will be very helpful.

    0 讨论(0)
  • 2020-11-29 16:50

    It's very simple steps to push your node js application from local to GitHub.

    Steps:

    1. First create a new repository on GitHub
    2. Open Git CMD installed to your system (Install GitHub Desktop)
    3. Clone the repository to your system with the command: git clone repo-url
    4. Now copy all your application files to this cloned library if it's not there
    5. Get everything ready to commit: git add -A
    6. Commit the tracked changes and prepares them to be pushed to a remote repository: git commit -a -m "First Commit"
    7. Push the changes in your local repository to GitHub: git push origin master
    0 讨论(0)
  • 2020-11-29 17:02

    We, the Javascript lovers, don't have to use Ruby (Jekyll or Octopress) to generate static pages in Github pages, we can use Node.js and Harp, for example:

    These are the steps. Abstract:

    1. Create a New Repository
    2. Clone the Repository

      git clone https://github.com/your-github-user-name/your-github-user-name.github.io.git
      
    3. Initialize a Harp app (locally):

      harp init _harp
      

    make sure to name the folder with an underscore at the beginning; when you deploy to GitHub Pages, you don’t want your source files to be served.

    1. Compile your Harp app

      harp compile _harp ./
      
    2. Deploy to Gihub

      git add -A
      git commit -a -m "First Harp + Pages commit"
      git push origin master
      

    And this is a cool tutorial with details about nice stuff like layouts, partials, Jade and Less.

    0 讨论(0)
  • 2020-11-29 17:04

    I was able to set up github actions to automatically commit the results of a node build command (yarn build in my case but it should work with npm too) to the gh-pages branch whenever a new commit is pushed to master.

    While not completely ideal as i'd like to avoid committing the built files, it seems like this is currently the only way to publish to github pages.

    I based my workflow off of this guide for a different react library, and had to make the following changes to get it to work for me:

    • updated the "setup node" step to use the version found here since the one from the sample i was basing it off of was throwing errors because it could not find the correct action.
    • remove the line containing yarn export because that command does not exist and it doesn't seem to add anything helpful (you may also want to change the build line above it to suit your needs)
    • I also added an env directive to the yarn build step so that I can include the SHA hash of the commit that generated the build inside my app, but this is optional

    Here is my full github action:

    name: github pages
    
    on:
        push:
            branches:
            - master
    
    jobs:
        deploy:
            runs-on: ubuntu-18.04
            steps:
            - uses: actions/checkout@v2
    
            - name: Setup Node
                uses: actions/setup-node@v2-beta
                with:
                node-version: '12'
    
            - name: Get yarn cache
                id: yarn-cache
                run: echo "::set-output name=dir::$(yarn cache dir)"
    
            - name: Cache dependencies
                uses: actions/cache@v2
                with:
                path: ${{ steps.yarn-cache.outputs.dir }}
                key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
                restore-keys: |
                    ${{ runner.os }}-yarn-
            - run: yarn install --frozen-lockfile
            - run: yarn build
                env:
                REACT_APP_GIT_SHA: ${{ github.SHA }}
    
            - name: Deploy
                uses: peaceiris/actions-gh-pages@v3
                with:
                github_token: ${{ secrets.GITHUB_TOKEN }}
                publish_dir: ./build
    

    Alternative solution

    The docs for next.js also provides instructions for setting up with Vercel which appears to be a hosting service for node.js apps similar to github pages. I have not tried this though and so cannot speak to how well it works.

    0 讨论(0)
  • 2020-11-29 17:09

    No, You cannot publish on Github pages. Try Heroku or something like that.

    0 讨论(0)
提交回复
热议问题