Home >Web Front-end >Front-end Q&A >tomcat deploy nodejs

tomcat deploy nodejs

WBOY
WBOYOriginal
2023-05-27 19:39:361098browse

As Web applications continue to develop and evolve, more and more developers are beginning to use a variety of technologies to build more complex Web applications. Among them, Node.js has become an indispensable component, which can provide multiple advantages such as efficient and scalable non-blocking I/O operations and seamless integration with JavaScript development.

However, for those applications running on Java web servers, how to seamlessly integrate with Node.js is a big problem. Fortunately, with the development of Apache Tomcat and Node.js, it is now possible to deploy Node.js on Tomcat in a relatively simple way.

This article will introduce the specific steps to deploy Node.js in Tomcat, as well as the required tools and precautions.

Prerequisites

Before starting to introduce the specific deployment steps, we need to ensure that the following prerequisites are met:

1. Install Node.js and npm

Make sure you have successfully installed Node.js and npm on the Tomcat server. On Linux systems, you can install it using the following command:

sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npm

2. Download and install the Tomcat Native library

Tomcat Native is a set of optional add-ons that provide high-performance HTTP Connectors and other native APIs. The following are the steps to install Tomcat Native:

  • Download Tomcat Native binary file

You can download the corresponding version from the link below:

https: //tomcat.apache.org/download-native.cgi

  • Unzip the file

After you unzip the file, you can copy the JAR file to Tomcat Under the lib directory:

cp tomcat-native-1.x.xx-src/native/jni/native/.libs/*.jar $CATALINA_BASE/lib
  • Compile Tomcat Native

Enter the decompressed Tomcat Native source code directory and use the following command to compile:

./configure --with-apr=(your apr-config path) --with-ssl=(your openssl path)
make
sudo make install
  • Modify Tomcat configuration file

Open Tomcat’s configuration file conf/server.xml and find the following line:

<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />

Replace it with the following content:

<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true">
    <SSLHostConfig>
        <Certificate certificateKeystoreFile="(Your keystore file path)"
                     type="RSA" />
    </SSLHostConfig>
</Connector>

After the installation is complete, you can start deploying the Node.js application to Tomcat.

Deploy Node.js application to Tomcat

Step 1: Create a folder

First, we need to create a folder in Tomcat to store our Node.js application. In the Tomcat installation directory, find the webapps directory and create a folder named node.

Step 2: Write the package.json file

We need to write a package.json file in the application root directory to describe the dependencies of the application and Node.js to run entry file.

Open the command line and enter the application root directory, and then use npm to initialize the package.json file of the current directory:

npm init -y

This will automatically create a basic package.json file, which you can follow You need to modify the fields in it.

Next, we need to add the following two entries to the package.json file:

{
  ...
  "main": "index.js",
  "dependencies": {
    "express": "^4.16.4"
  }
}

Among them, the main field points to the entry file of the Node.js application, and the dependencies field describes the application. Dependencies. In the above example, we added an application created using the Express framework and specified the corresponding version.

Step 3: Write Node.js application

Next, we write the entry file index.js of the Node.js application in the root directory. The following is a simple Express application:

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('Hello World!');
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

The above application simply listens to port 3000 and returns "Hello World!" when accessing the homepage.

Step 4: Create web.xml file

In Tomcat, we must define a web.xml file for each web application. In the WEB-INF directory of the application, create a file named web.xml and add the following content:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE web-app PUBLIC
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
  <context-param>
      <param-name>contextPath</param-name>
      <param-value>/node</param-value>
  </context-param>
  <servlet>
    <servlet-name>Node</servlet-name>
    <servlet-class>org.apache.catalina.servlets.WebdavServlet</servlet-class>
    <init-param>
      <param-name>aliases</param-name>
      <param-value>/|file:///source/node</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>Node</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

The above web.xml file has the following two important parts:

  • It will let Tomcat know how to set the context path for our web application.
  • It configures a servlet named Node, which associates the application's alias and application root directory.

Among them, the /file:///source/node path in the param-value field should be replaced with the actual root directory path of your Node.js application.

Step Five: Deploy Node.js Application

Now, our application is ready to be deployed. Copy the application's root directory to the web application's root directory (WEB-INF/classes/).

Step 6: Start Tomcat

Now, start Tomcat and visit the application's URL address: http://localhost:8080/node, you should be able to see "Hello" in the browser World!" string.

Summary

This article introduces how to deploy Node.js applications in Tomcat, including installing the Tomcat Native library and Node.js and their dependencies, writing package.json files, and creating web. xml file, and deploy the application into Tomcat.

If you need to implement a web application using Node.js in a Java web server, then the steps and tips provided in this article should help you achieve this goal easily.

The above is the detailed content of tomcat deploy nodejs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn