表达节点js无效的状态码范围错误

时间:2020-01-30 16:58:00

标签: node.js express

嗨,大家好,我是Node js的新手,最近我遇到了这个无效的状态代码错误。 RangeError [ERR_HTTP_INVALID_STATUS_CODE]:无效的状态码:18。我不知道如何解决这个问题。任何建议或帮助,将不胜感激!代码如下!祝您新年快乐

app.js

const express = require("express");
const app = express();
const Post = require("./models/post.js");
// import body-parser middleware
const bodyParser = require("body-parser");
const urlencodedParser = bodyParser.urlencoded({ extended: false });
// use the middleware
app.use(bodyParser.json());
app.use(urlencodedParser); // parse application/x-www-form-urlencoded

app.post("/posts/",(req,res) =>{
  Post.insert(req.body,(error,Post)=>{
    if(error){
      console.log(error)
      res.status(500).send()
      return
    }
    res.status(200).send(Post)
  })
})

module.exports = app;

post.js

const db = require('../db');
var post = {


    insert: function (post, callback) {
        const insertQuery = `INSERT INTO post (text_body, fk_poster_id) VALUES (?,?);`;
        db.query(insertQuery, [post.text_body, post.fk_poster_id], (error, results) => {
            if (error) {
                callback(error, null);
                return;
            }
            callback(null, results.insertId);
        });
    }
}

module.exports = post;

sql代码

CREATE DATABASE friendbook;
use friendbook;
        select* from post;
    select * from friendship;
    select text_body,created_at from post where id = 1;
INSERT INTO post (fk_poster_id, text_body) VALUES (4, 'I love going to the beach!');
select * from post;
INSERT INTO post (fk_poster_id, text_body) VALUES (999, 'Great weather today!');
UPDATE user SET full_name = 'Dexter Ng' WHERE id = 2;
SELECT * FROM user WHERE id = 2;
select * from post;
delete from post where id = 2;
select * from likes;
INSERT INTO likes
(fk_user_id, fk_post_id)
VALUES
(4, 3);
INSERT INTO likes
(fk_user_id, fk_post_id)
VALUES
(2, 999);
INSERT INTO friendship
(fk_friend_one_id, fk_friend_two_id)
VALUES
(2, 3);
SELECT * FROM friendship
WHERE
fk_friend_one_id = 2
AND
fk_friend_two_id = 3;
SELECT * FROM friendship
WHERE
fk_friend_one_id = 3
AND
fk_friend_two_id = 2;

INSERT INTO friendship
(fk_friend_one_id, fk_friend_two_id)
VALUES
(3, 2);
select * from friendship;

delete from friendship where id = 9 or id = 10;
SELECT * FROM friendship;
SELECT * FROM friendship, user
where friendship.fk_friend_one_id = user.id;

CREATE TABLE user (
    id INT NOT NULL AUTO_INCREMENT,
    full_name varchar(255) NOT NULL,
    username varchar(255) NOT NULL,
    bio varchar(255) NOT NULL DEFAULT '',
    date_of_birth Date NULL,
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    PRIMARY KEY (id),
    UNIQUE (username)
);

CREATE TABLE post (
    id INT NOT NULL AUTO_INCREMENT,
    fk_poster_id INT NOT NULL,
    text_body TEXT NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    PRIMARY KEY (id),
    FOREIGN KEY (fk_poster_id) REFERENCES user(id) ON DELETE CASCADE
);


CREATE TABLE likes (
    id INT NOT NULL AUTO_INCREMENT,
    fk_user_id INT NOT NULL,
    fk_post_id INT NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    PRIMARY KEY (id),
    UNIQUE KEY (fk_user_id, fk_post_id),
    FOREIGN KEY (fk_user_id) REFERENCES user(id) ON DELETE CASCADE,
    FOREIGN KEY (fk_post_id) REFERENCES post(id) ON DELETE CASCADE
);

CREATE TABLE friendship (
    id INT NOT NULL AUTO_INCREMENT,
    fk_friend_one_id INT NOT NULL,
    fk_friend_two_id INT NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    PRIMARY KEY (id),
    UNIQUE KEY (fk_friend_one_id, fk_friend_two_id),
    FOREIGN KEY (fk_friend_one_id) REFERENCES user(id),
    FOREIGN KEY (fk_friend_two_id) REFERENCES user(id)
);





use friendbook;

INSERT INTO user (full_name, username, bio, date_of_birth)
VALUES
('Johnny Appleseed', 'johnny_appleseed', 'This is John\'s bio!', '1993-10-19');

INSERT INTO user (full_name, username, bio, date_of_birth)
VALUES
('Dexter', 'dexter', 'I love technology!', '1995-10-01');

INSERT INTO user (full_name, username, bio, date_of_birth)
VALUES
('Ron Macdonald', 'ron_mac', 'Ron. CEO. Father.', '1969-01-01');

INSERT INTO user (full_name, username, bio, date_of_birth)
VALUES
('Chad Thunder', 'chad_thunder', 'This is Chad!', '1980-10-10');

SELECT id INTO @johnny_id FROM user WHERE full_name = 'Johnny Appleseed' LIMIT 1;
SELECT id INTO @dexter_id FROM user WHERE full_name = 'Dexter' LIMIT 1;
SELECT id INTO @ron_id FROM user WHERE full_name = 'Ron Macdonald' LIMIT 1;
SELECT id INTO @chad_id FROM user WHERE full_name = 'Chad Thunder' LIMIT 1;

# friendship between Johnny Appleseed and Dexter
INSERT INTO friendship (fk_friend_one_id, fk_friend_two_id) VALUES (@johnny_id, @dexter_id);
INSERT INTO friendship (fk_friend_one_id, fk_friend_two_id) VALUES (@dexter_id, @johnny_id);

# friendship between Johnny Appleseed and Ron Macdonald
INSERT INTO friendship (fk_friend_one_id, fk_friend_two_id) VALUES (@johnny_id, @ron_id);
INSERT INTO friendship (fk_friend_one_id, fk_friend_two_id) VALUES (@ron_id, @johnny_id);

# friendship between Johnny Appleseed and Chad Thunder
INSERT INTO friendship (fk_friend_one_id, fk_friend_two_id) VALUES (@johnny_id, @chad_id);
INSERT INTO friendship (fk_friend_one_id, fk_friend_two_id) VALUES (@chad_id, @johnny_id);

# friendship between Dexter and Chad Thunder
INSERT INTO friendship (fk_friend_one_id, fk_friend_two_id) VALUES (@dexter_id, @chad_id);
INSERT INTO friendship (fk_friend_one_id, fk_friend_two_id) VALUES (@chad_id, @dexter_id);

# posts by Johnny
INSERT INTO post (fk_poster_id, text_body, created_at) VALUES (@johnny_id, 'Hello, world!', NOW() - INTERVAL 3 DAY);
SELECT LAST_INSERT_ID() INTO @first_post_id;
INSERT INTO post (fk_poster_id, text_body, created_at) VALUES (@johnny_id, 'I love the weather in Singapore.', NOW() - INTERVAL 2 DAY);


# posts by Dexter
INSERT INTO post (fk_poster_id, text_body, created_at) VALUES (@dexter_id, 'I would go out tonight, but I haven\'t got a stitch to wear.', NOW() - INTERVAL 3 DAY);
SELECT LAST_INSERT_ID() INTO @fourth_post_id;
INSERT INTO post (fk_poster_id, text_body, created_at) VALUES (@dexter_id, 'We can go for a walk where it\'s quiet and dry.', NOW() - INTERVAL 2 DAY);
INSERT INTO post (fk_poster_id, text_body, created_at) VALUES (@dexter_id, 'I listen to The Smiths btw.', NOW() - INTERVAL 1 DAY);

# posts by Ron
INSERT INTO post (fk_poster_id, text_body, created_at) VALUES (@ron_id, 'I can recite pi to the 50th decimal place.', NOW() - INTERVAL 1 DAY);

# posts by Chad
INSERT INTO post (fk_poster_id, text_body, created_at) VALUES (@chad_id, 'Work hard play hard!', NOW() - INTERVAL 1 DAY);


# insert likes
INSERT INTO likes (fk_user_id, fk_post_id) VALUES (@dexter_id, @first_post_id);
INSERT INTO likes (fk_user_id, fk_post_id) VALUES (@chad_id, @first_post_id);

use friendbook;

ALTER TABLE user ADD COLUMN password VARCHAR(255) NOT NULL;

select * from user;

0 个答案:

没有答案
相关问题