閱讀英文 編輯

共用方式為

Build your first app using Node.js

Build your first Node.js app

After you've cloned the repo for Node.js sample app, you can build and test the app in your local environment.

In this page, you'll learn to:

  1. Build and run your first app
  2. Test your sample app

Build and run the sample

After the repository is cloned, you can build the sample app.

  1. Open a terminal window.

  2. Run the change directory command in the terminal to change to the sample app directory:

    cd Microsoft-Teams-Samples/samples/app-hello-world/nodejs/
    
  3. Install all dependencies using the following command:

    npm install
    

    You should see a bunch of dependencies getting installed.

  4. Run the app using the following command after installation is complete:

    npm start
    

    When the Hello World app starts, it displays App started listening on port 3333 in the terminal window.

    注意

    If you see a different port number displayed in the message above, it is because you have a PORT environment variable set. You can continue to use that port or change your environment variable to 3333.

  5. Open a browser window and use the following URLs to verify that all the Hello World app URLs are loading:

    • http://localhost:3333

      Screenshot of image showing Node.js local host.

    • http://localhost:3333/hello

      Screenshot of image showing Node.js local host hello page.

    • http://localhost:3333/first

      Screenshot of image showing Node.js local host first tab.

    • http://localhost:3333/second

      Screenshot of image showing Node.js local host second tab.

Test your sample app in local environment

Remember that apps in Microsoft Teams are web applications exposing one or more capabilities. Make your app available on the internet so that the Teams platform can load it. To make your app reachable from the internet, you need to host your app.

For local testing, you can run the app on your local machine and create a tunnel to it with a web endpoint. ngrok is a free tool that lets you do just that. With ngrok, you can get a web address such as https://d0ac14a5.ngrok.io (this URL is just an example). You can download and install ngrok for your environment. Make sure you add it to a location in the PATH.

Create ngrok tunnel

After you install ngrok, you can create a tunnel to deploy your app locally.

  1. Open a new terminal window.

  2. Run the following command to create a tunnel. The sample app uses port 3333:

    ngrok http 3333 --host-header=localhost:3333
    

    An ngrok tunnel is created. Here's an example of ngrok tunnel.

    Screenshot of image showing ngrok tunnel.

    Ngrok listens to requests from the internet and routes them to your app running on port 3333.

To verify the app's local deployment:

  1. Open the browser.

  2. Load your app using the following URL:

    https://<forwarding address in ngrok console session>/

    Here's an example of the URL:

    Screenshot of image showing node.js app running locally on ngrok tunnel.

    You can try adding hello, first, or second to the ngrok tunnel url to view different tab pages of the app.

    Ngrok listens to requests from the internet and routes them to your app running on port 3333.

  3. Make a note of the forwarding address in ngrok console. You need this URL to deploy your app in Teams.

    注意

    If you used a different port during build and run, make sure you use the same port number to setup the ngrok tunnel.

    提示

    It's a good idea to run ngrok in a different terminal window to keep it running without interfering with the node app, which you might later have to stop, rebuild, and rerun. The ngrok session returns useful debugging information in this window. The paid version of ngrok allows persistent names.

    重要

    If you use the free version of ngrok, your app is available only during the current session on your development machine. It isn't available if the machine is shut down or goes to sleep. When you restart the service, it returns a new address. Then, you must update every location that uses the outdated address. Remember this step when sharing the app for testing.

Build Node.js app package

You can use gulp CLI to build the app package for Node.js.

  1. Open a terminal window.

  2. Ensure that you are in the directory where the node.js is cloned. This is the folder where gulpfile.js is placed.

  3. Run the following command to build the Node.js app package.

    $ gulp

    Here's an example of the command output:

    [13:39:27] Using gulpfile ~\documents\github\msteams-samples-hello-world-nodejs\gulpfile.js

    [13:39:27] Starting 'clean'...

    [13:39:27] Starting generate-manifest'...

    [13:39:27] Finished 'generate-manifest' after 11 ms

    [13:39:27] Finished 'clean after 21 ms

    [13:39:27] Starting 'default'... Build completed. Output in manifest folder

    [13:39:27] Finished 'default' after 62 μs

    The app package helloworldapp.zip is created. You can find the package file at the following path.

    <path to the cloned repo>/Microsoft-Teams-Samples/samples/app-hello-world/nodejs/manifest

    注意

    Search for the app package file, helloworldapp.zip, if the location isn't clear in the tool you're using.