sp-pnp-node icon indicating copy to clipboard operation
sp-pnp-node copied to clipboard

Receiving 401 when inside a container

Open chanm003 opened this issue 10 months ago • 0 comments

On the left terminal, I am running an Express Server that successfully queries an on-premises server using PnP JS.

On the right terminal, I containerize this Express server, and receive a HTTP 401 Unauthorized. Same JS file used in both instances. Can anybody please help? 401-when-express-server-is-containerized

index.js

const express = require('express');
const pnp = require('@pnp/sp');
const { PnpNode } = require('sp-pnp-node');

const app = express();

pnp.sp.setup({
  sp: {
      fetchClientFactory: () => {
          return new PnpNode({
              siteUrl: 'http://40.111.32.91',
              authOptions: {
                  username: 'SP2016-FARM-ADMIN',
                  domain: 'CONTOSO',
                  password: '6tfc0okm<LP_'
              }
          })
      },
      baseUrl: 'http://40.111.32.91'
  }
});

app.get('/', async (req, res) => {
  const lists = await pnp.sp.web.lists.get();
  console.log(`Number of lists: ${lists.length}`)
  res.json({ username: `${lists.length}` });
});

app.listen(8880, () => console.log('Server on port 8880.'));

docker-compose.yml

version: '3'
services:
  nodejs_api:
    image: docker-pnpjs
    container_name: docker-pnpjs
    build:
      context: .
      dockerfile: Dockerfile
    env_file: ./docker-compose-backend.env
    ports:
      - "8880:8880"
    command: npm run dev
    volumes:
      - /app/node_modules
      - .:/app

Dockerfile

FROM node:16

RUN ["apt-get", "update"]
RUN ["apt-get", "install", "-y", "vim"]

WORKDIR /app

COPY package*.json ./

RUN npm install
RUN npm install -g nodemon

COPY . .

EXPOSE 8880

package.json

{
    "name": "docker-pnpjs",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "start": "node index.js",
        "dev": "nodemon --legacy-watch index.js",
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "dependencies": {
        "@pnp/pnpjs": "^1.3.11",
        "express": "^4.17.1",
        "sp-pnp-node": "^3.0.1"
    },
    "devDependencies": {
        "nodemon": "^2.0.12"
    },
    "author": "Mike Chan",
    "license": "ISC"
}

chanm003 avatar Aug 23 '23 14:08 chanm003